我试图将一个从HTML表单上传的文件通过POST上传至Google Drive.我能够得到文件名、文件临时路径,但它却抛出了这个错误,文件最终没有上传到Google Drive。
@method_decorator(csrf_exempt, name='dispatch')
class Submit(MissionSubmission):
def get(self, request, mission_id):
mission = MissionDetail.objects.all().filter(id=mission_id)[0]
mission = newline_aware(mission)
return render(request, 'home/submit.html', {'mission': mission})
def post(self, request, mission_id):
request.upload_handlers = [TemporaryFileUploadHandler(request=request)]
return self._post(request, mission_id)
@method_decorator(csrf_protect)
def _post(self, request, mission_id):
uploaded_file = request.FILES['filename']
print(uploaded_file.name)
print(uploaded_file.size)
print(uploaded_file.temporary_file_path())
print(uploaded_file.content_type)
file_metadata = {'name': uploaded_file.name}
media = MediaFileUpload(uploaded_file.temporary_file_path,
mimetype=uploaded_file.content_type)
file = drive.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('File ID: %s' % file.get('id'))
return HttpResponse("Hi")
有人知道是什么问题吗?这是我从控制台得到的结果。
(venv) marcus@marcus-RAVEN-SE:/mnt/d/Programming/UnlockProject/System/unlock$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
May 08, 2020 - 17:44:18
Django version 3.0.5, using settings 'unlock.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/May/2020 17:44:22] "GET / HTTP/1.1" 200 8283
[08/May/2020 17:44:22] "GET /static/js/bootstrap.js HTTP/1.1" 200 135079
[08/May/2020 17:44:22] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 196315
[08/May/2020 17:44:22] "GET /static/images/CYCLogo.png HTTP/1.1" 304 0
[08/May/2020 17:44:22] "GET /static/images/TitleLogo.png HTTP/1.1" 200 110175
[08/May/2020 17:44:22] "GET /static/images/unlockBG.jpg HTTP/1.1" 200 649451
[08/May/2020 17:44:26] "GET /mission/ HTTP/1.1" 200 6334
[08/May/2020 17:44:26] "GET /static/images/Lock.png HTTP/1.1" 304 0
[08/May/2020 17:44:27] "GET /mission/submit/1 HTTP/1.1" 200 4496
Login.png
258917
/tmp/tmpnpz7epou.upload.png
image/png
Internal Server Error: /mission/submit/1
Traceback (most recent call last):
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/unlock/home/views.py", line 116, in post
return self._post(request, mission_id)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/unlock/home/views.py", line 127, in _post
media = MediaFileUpload(uploaded_file.temporary_file_path,
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/mnt/d/Programming/UnlockProject/System/venv/lib/python3.8/site-packages/googleapiclient/http.py", line 575, in __init__
fd = open(self._filename, "rb")
TypeError: expected str, bytes or os.PathLike object, not method
[08/May/2020 17:44:33] "POST /mission/submit/1 HTTP/1.1" 500 130268
Not Found: /favicon.ico
[08/May/2020 17:44:34] "GET /favicon.ico HTTP/1.1" 404 2844
由于我能够得到 uploaded_file的详细信息,所以我认为django部分的代码没有问题,但导致这个错误的是Google部分的代码。但这真的让我很无奈,我不知道是哪里出了问题,一切看起来都和Google文档中的一样,都是正确的。
更新:我试着把这个变量替换成一个实际的文件路径,结果成功了。那么......是不是Django提供的 uploaded_file.temporary_file_path()无效?
好吧,看了你的代码和错误信息,我很清楚,你其实是在传递函数(可调用对象)。temporary_file_path
而非 return
该函数的。
基本上
temporary_file_path != temporary_file_path()
一个(第一个)是函数对象,另一个是实际的文件路径。
把你的代码改成这样,你应该就可以了。
media = MediaFileUpload(uploaded_file.temporary_file_path(),
mimetype=uploaded_file.content_type)