![](/img/trans.png)
[英]How to upload a file to Google Cloud Storage on Python 3?
[英]File Upload To Google Cloud Storage on Appengine(Python)
我一直在嘗試從我的Google Appengine應用程序將文件上傳到Google雲存儲。 但我不斷收到此錯誤消息:
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/cgi.py", line 540, in __getitem__
raise KeyError, key KeyError: 'files'
這是我的客戶端上傳表格:
<form id="fileupload" action="/uploads" method="POST" enctype="multipart/form-data">
<!-- Redirect browsers with JavaScript disabled to the origin page -->
<noscript><input type="hidden" name="redirect" value="http://2479ja.tv/jqueryupload"></noscript>
<!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
<div class="row fileupload-buttonbar">
<div class="col-lg-7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
<input type="file" name="files" value="" required="required">
</span>
<button type="submit" class="btn btn-primary start">
<i class="glyphicon glyphicon-upload"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel upload</span>
</button>
<button type="button" class="btn btn-danger delete">
<i class="glyphicon glyphicon-trash"></i>
<span>Delete</span>
</button>
<input type="checkbox" class="toggle" value="">
<!-- The global file processing state -->
<span class="fileupload-process"></span>
</div>
<!-- The global progress state -->
<div class="col-lg-5 fileupload-progress fade">
<!-- The global progress bar -->
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar progress-bar-success" style="width:0%;"></div>
</div>
<!-- The extended global progress state -->
<div class="progress-extended"> </div>
</div>
</div>
<!-- The table listing the files available for upload/download -->
<table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
</form>**
這是我的服務器端python腳本:
class UploadHandler2(webapp.RequestHandler):
def PrintWithCarriageReturn(s):
sys.stdout.write('\r' + s)
sys.stdout.flush()
def post(self):
form = cgi.FieldStorage(keep_blank_values = 1)
print form.keys()
fileitem = form['files']
if fileitem.filename:
filename = os.path.basename(fileitem.filename)**
有人可以告知此代碼有什么問題嗎? 謝謝
您正在使用webapp來處理您的請求,看起來您的cgi.FieldStorage沒有收到數據,因此表單對象中沒有“文件”,因此沒有KeyError。
對於webapp,您可以執行以下操作:
def post(self):
fileitem = self.request.POST.get('files', None)
fileblob = fileitem.file.read()
filename = fileitem.filename
mimetype = fileitem.type
(當然,通過適當的錯誤檢查是否為None)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.