我目前正在开发一个将使用Google云端存储上传用户文档的网络应用。但是,我遇到了障碍。当我尝试继续上传时,我不断获得[Errno 2] No such file or directory: 'Marty McFly-Paycheck-06_ConditionalDirectives.pdf'
。这个错误令我感到困惑,因为我认为该文件将直接从我的表单文件上传中获取。
假设这个我错了吗?我应该保存然后以某种方式提供文件吗?
将文档上传到云存储的当前代码
#Uploading Documents function
def upload_documents(bucket_name,source_file_name,destination_blob_name):
storage_client = storage.Client()
storage_bucket = storage_client.get_bucket(bucket_name)
blob = storage_bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print("Document Uploaded")
from .forms import DocumentUpload
# Create your views here.
def get_upload(request):
# If this is a POST request, we will process the data -- sending it to Google Cloud Storage
if request.method == 'POST':
#create a form instance populated with data -- (Bound to the data previously entered that might need correction)
name = request.POST['name']
titleofdocument = request.POST['titleofdocument']
doc_file = request.POST['doc_file']
fullUpload = name + "-" + titleofdocument + "-" + doc_file
print(titleofdocument,name,doc_file)
print(fullUpload)
form = DocumentUpload(request.POST)
upload_documents("upload_documents",fullUpload,"SampleBlob")
else:
form = DocumentUpload()
return render(request, 'document-upload.html',{'form':form})
有问题的完整堆栈跟踪
Marty McFly-Paycheck-06_ConditionalDirectives.pdf
Internal Server Error: /upload-documents/
Traceback (most recent call last):
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Joel\Documents\dev-projects\greaterdemand\documents\views.py", line 40, in get_upload
upload_documents("upload_documents",fullUpload,"SampleBlob")
File "C:\Users\Joel\Documents\dev-projects\greaterdemand\documents\views.py", line 23, in upload_documents
blob.upload_from_filename(source_file_name)
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\google\cloud\storage\blob.py", line 988, in upload_from_filename
with open(filename, 'rb') as file_obj:
FileNotFoundError: [Errno 2] No such file or directory: 'Marty McFly-Paycheck-06_ConditionalDirectives.pdf'
[20/Jan/2018 10:54:03] "POST /upload-documents/ HTTP/1.1" 500 78673
如果有人有任何有用的文档或建议的链接,我将非常感谢您的帮助。谢谢!
答案 0 :(得分:1)
上传的文件位于request.FILES
。您没有将文件传递给表单。试试这个。
form = DocumentUpload(request.POST, request.FILES)