python - 结合requestHandler和BlobstoreUploadHandler的python GAE问题

  显示原文与译文双语对照的内容

<form action="/fileupload" enctype="multipart/form-data" method="post">
 <label>Title<input name="name"></label>
 <label>Author<input name="author"></label>
 <label>Year <input name="year"></label>
 <label>Link <input name="link"></label>
 <input type="file" name="file">
 <input type="submit">
</form>

问题是 self.get_uploads()self.request.get('file') 工作( 或者我建议它工作) 时为什么不返回任何


class FileUploadHandler(H.Handler,blobstore_handlers.BlobstoreUploadHandler):
def post(self):
 name=self.request.get('name')
 if name:
 logging.error(name)
 author=self.request.get('author')
 if author:
 logging.error(author)
 year =self.request.get('year')
 if year and year.isdigit():
 year=int(year)
 f =self.get_uploads('file')#FILE NOT FOUND
 #f=self.request.get('file') # WORKS FINE

 if not f:
 logging.error("FILE NOT FOUND")
 self.redirect("/files")

我查看了样例应用程序,但它们也使用了

答案是,你应该为blob创建上传 url 。

时间: 作者:

你需要 get_uploads()[0] 或者名称。

上传处理程序( 比较并在代码中进行适当的更改)的简单示例:


 <form name="myform" action="{{ upload_url }}" method="post" enctype="multipart/form-data">
 <h1>Select an Image</h1>
 <input name="file" type="file"><br>
 <input type="submit" value="Upload">
 </form>

处理程序:


class UploadBlobsHandler(blobstore_handlers.BlobstoreUploadHandler):

 def post(self):
 try:
 upload = self.get_uploads()[0]
 logging.info(upload)
 url = images.get_serving_url(upload)
 # Do something with it.
 except:
 self.redirect('/uploadform/?error', abort=True)

 self.redirect('/uploadform/?success&image_url=' + url)

关于如何在blobstorrhandlers中使用上载的示例: gae-image-upload-example

作者:

这是我的问题的完整解决方案:


 <form action="{{upload_url}}" enctype="multipart/form-data" method="post">
 <label>Title<input name="name"></label>
 <label>Author<input name="author"></label>
 <label>Year <input name="year"></label>
 <label>Link <input name="link"></label>
 <input type="file" name="file">
 <input type="submit">
</form> 

火警的警铃在哪里? upload_url =blobstore.create_upload_url('/fileupload',max_bytes_per_blob=10*1000*1000) 下面是处理请求的代码:


upload=self.get_uploads('file')[0]

作者:
...