0
我想從我的DRF後端上傳一個.ipa文件到使用python Multipart數據編碼器的第三方應用程序(http://toolbelt.readthedocs.io/en/latest/uploading-data.html)。不過,我收到以下錯誤 -請求使用CSRF失敗的toolbelt多部分上傳文件
( '連接中斷。',BrokenPipeError(32 '斷管'))
如果我刪除了 'RB' 屬性,我得到的以下錯誤instead-
'UTF-8' 編解碼器在10位機不能解碼字節0xe3:無效延續字節
可能有人請點OU這裏有什麼不對?順便說一句,我決定使用請求工具欄,因爲我可能會上傳大文件。
from django.views.generic import View
from django.conf import settings
import os
import requests #sudo pip install requests, it's an external library
from requests_toolbelt.multipart.encoder import MultipartEncoder #pip install requests-toolbelt
class upload_binary(generics.GenericAPIView):
def post(self, request, format=None):
URL = "http://localhost:9020/"
csrf = requests.get(URL).cookies['csrftoken']
post_url = "http://localhost:9020/upload/"
upload_file_name = "SomeApp.ipa"
media_dir = settings.MEDIA_ROOT
upload_file_path = os.path.join(media_dir, upload_file_name)
filedata = MultipartEncoder(fields = {
'csrfmiddlewaretoken': csrf, 'file1': ('file', open(upload_file_path, 'rb'))
})
headersdict = {'X-CSRFToken': csrf, 'Content-Type': filedata.content_type}
upload_bin_req = requests.post(post_url, data = filedata, headers = headersdict)
return JsonResponse({})