form 表單上傳文件及傳輸數據的編碼格式


form中的 action  控制請求往什么地方提交

     method  請求方式 如果不寫默認是get 請求

    如果想傳文件 必須要把默認的urlencoded的改成enctype="multipart/form-date"

 

后端的代碼

def upload_file(request):
    if request.method == 'POST':
        print('path:',request.path)
        print('full_path:',request.get_full_path())
        # print(request.FILES)
        file_obj = request.FILES.get('my_file')
        print(file_obj.name)
        with open(file_obj.name,'wb') as f:
            for line in file_obj.chunks():
                f.write(line)
        return HttpResponse('OK')
    return render(request,'index.html')

 前后端傳輸數據編碼格式contentType

  urlencoded

     對應的數據格式 :name=jason&password=666

     后端獲取數據: rrequest.POST

  formdate

     form 表單傳輸文件的編碼格式

     后端獲取文件格式數據:request.FILES

     后端獲取普通鍵值對數據: request.POST


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM