2013-12-17 216 views
0

我一直在試圖將文件上傳到從我的谷歌AppEngine應用程式Google雲存儲。但我不斷收到此錯誤信息:文件上傳到谷歌雲存儲AppEngine上(Python)的

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">&nbsp;</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)** 

可有人請指教一下這段代碼有錯嗎?由於

回答

1

您使用的Web應用程序來處理您的請求,它看起來像你的cgi.FieldStorage未接收到數據,所以沒有「文件」表單中的對象,因此KeyError異常。

對於web應用程序,你可以做:

def post(self): 
    fileitem = self.request.POST.get('files', None) 

    fileblob = fileitem.file.read() 
    filename = fileitem.filename 
    mimetype = fileitem.type 

(具有相應的錯誤檢查無,當然)。

+0

@codegeek能夠解決這個問題,但現在有以下錯誤消息 OSERROR:[錯誤2]沒有這樣的文件或目錄。的FileItem = self.request.POST.get( '文件',無) fileblob = fileitem.file.read() \t文件名= fileitem.filename \t \t \t服務= get_authenticated_service2() \t媒體= MediaFileUpload (文件名,CHUNKSIZE = CHUNKSIZE,可恢復=真) \t的#if不media.mimetype(): \t媒體= MediaFileUpload(文件名,DEFAULT_MIMETYPE,可恢復=真) \t請求= service.objects()插入(桶= bucket_name,name = object_name,media_body = media)。請指教。謝謝 – user3112567

相關問題