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

猜你喜欢

转载自www.cnblogs.com/HUIWANG/p/11028961.html
今日推荐