如何上传多个文件到BlobStore?
我正在尝试将表单中的多个文件上传到 BlobStore。
表单:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
<label>Key Name</label><input type="text" name="key_name" size="50"><br/>
<label>name</label><input type="text" name="name" size="50"><br/>
<label>image</label><input type="file" name="image" size="50"><br/>
<label>thumb</label><input type="file" name="thumb" size="50"><br/>
<input type="submit" name="submit" value="Submit">
</form>
然后我尝试为每个上传的文件获取 BlobInfo 对象:
def post(self):
image_upload_files = self.get_uploads('image')
thumb_upload_files = self.get_uploads('thumb')
image_blob_info = image_upload_files[0]
thumb_blob_info = thumb_upload_files[0]
我看到一些奇怪的行为。这两个文件都将其放入 BlobStore,但我不知道如何获取密钥以便将它们存储在另一个实体上。上面的代码成功获取了 image_blob_info 的密钥,但没有获取到thumb_blob_info 的密钥。我不明白如何使用 get_uploads。我想通过表单传递多个文件,然后按名称获取它们,以便我可以将它们存储在另一个实体上适当的 BlobReferenceProperties 中。
I'm trying to upload multiple files in a form to the BlobStore.
Form:
<form action="{{upload_url}}" method="POST" enctype="multipart/form-data">
<label>Key Name</label><input type="text" name="key_name" size="50"><br/>
<label>name</label><input type="text" name="name" size="50"><br/>
<label>image</label><input type="file" name="image" size="50"><br/>
<label>thumb</label><input type="file" name="thumb" size="50"><br/>
<input type="submit" name="submit" value="Submit">
</form>
I'm then trying to fetch the BlobInfo objects for each of those files uploaded:
def post(self):
image_upload_files = self.get_uploads('image')
thumb_upload_files = self.get_uploads('thumb')
image_blob_info = image_upload_files[0]
thumb_blob_info = thumb_upload_files[0]
I'm seeing some weird behavior. Both files are making it into the BlobStore, but I cannot figure out how to get the Keys so that I can store those on another Entity. The code above manages to get the key for image_blob_info, but not thumb_blob_info. I don't understand how to use get_uploads. I want to pass multiple files through the form and then fetch them by name so I can store them in the appropriate BlobReferenceProperties on another Entity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个文件都需要自己唯一的上传 url,所以我猜测当所有三个文件都发布到同一个 url 时,会发生一些奇怪的事情。
Nick Johnson 的博客文章中描述了支持多个文件上传的最佳解决方案:
http://blog.notdot.net/2010/04/Implementing-a-dropbox-service-with-the-Blobstore-API-part-3 -多重上传支持
Each file needs its own unique upload url, so my guess is something wacky is happening when all three files are posted to the same url.
The best solution for supporting multiple file uploads is described in Nick Johnson's blog post:
http://blog.notdot.net/2010/04/Implementing-a-dropbox-service-with-the-Blobstore-API-part-3-Multiple-upload-support
您可以将文件发布到相同的名称,后跟
[]
,这将发布一个数组:然后在您的表单处理程序中,您可以像这样(取决于您的 Web 框架):
You could post the files to the same name, followed by
[]
, which will post an array:Then in your form handler, you can something like this (depending on your web framework):
使用最新版本的 plupload,我能够通过这段代码让 UploadQueue 与 GAE 一起使用。请注意,它是 CoffeeScript,但如果您确实需要,应该很容易转换回 JavaScript。它假设您从服务器返回一些 json 作为 {url:"gae generated url"}
Using the latest version of plupload, I was able to get the UploadQueue to work with GAE with this bit of code. Note, it is CoffeeScript, but should be easy to convert back to JavaScript if you really need to. It assumes you get a bit of json back from your server as {url:"gae generated url"}