Django / Dropbox上传的文件为0个字节

时间:2019-06-17 10:40:08

标签: python django dropbox-api

不能弄清楚为什么我通过Django上传到Dropbox的文件始终为零字节。

超大型Django(使用原始Python),文件将正常上传。

我的HTML声明了enctype="multipart/form-data"

VIEWS.PY:

if cv_form.is_valid():
   cv_form.save(commit=True)

   # print(request.FILES['cv'].size) == 125898
   DOC = request.FILES['cv']
   PATH = f'/CV/{DOC}'
   upload_handler(DOC, PATH)

   #http_response_happens_here()

处理程序:

def upload_handler(DOC, PATH):
    dbx = dropbox.Dropbox(settings.DROPBOX_APP_ACCESS_TOKEN)
    dbx.files_upload(DOC.file.read(), PATH) 

    #file gets uploaded but always 0bytes in size

1 个答案:

答案 0 :(得分:0)

我不好!我怎么没看到它。

该解决方案可以防止其他人遇到相同的问题。我自己战斗了一段时间。

if cv_form.is_valid():
   cv_form.save(commit=False) # commit should be False before upload

   # print(request.FILES['cv'].size) == 125898
   DOC = request.FILES['cv']
   PATH = f'/CV/{DOC}'
   upload_handler(DOC, PATH)
   cv_form.save(commit=True) # now you can save the file.

   #http_response_happens_here()