如何上传多个文件到BlobStore?

发布于 2024-10-12 23:25:24 字数 1095 浏览 5 评论 0原文

我正在尝试将表单中的多个文件上传到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-10-19 23:25:24

每个文件都需要自己唯一的上传 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

时间你老了 2024-10-19 23:25:24

您可以将文件发布到相同的名称,后跟 [],这将发布一个数组:

<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="files[]" size="50"><br/>
  <label>image</label><input type="file" name="files[]" size="50"><br/> 
  <label>thumb</label><input type="file" name="thumb" size="50"><br/> 
  <input type="submit" name="submit" value="Submit">
</form>

然后在您的表单处理程序中,您可以像这样(取决于您的 Web 框架):

for uploaded_file in request.FILES.getlist('files'):
    #do something with uploaded_file

You could post the files to the same name, followed by [], which will post an array:

<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="files[]" size="50"><br/>
  <label>image</label><input type="file" name="files[]" size="50"><br/> 
  <label>thumb</label><input type="file" name="thumb" size="50"><br/> 
  <input type="submit" name="submit" value="Submit">
</form>

Then in your form handler, you can something like this (depending on your web framework):

for uploaded_file in request.FILES.getlist('files'):
    #do something with uploaded_file
蹲墙角沉默 2024-10-19 23:25:24

使用最新版本的 plupload,我能够通过这段代码让 UploadQueue 与 GAE 一起使用。请注意,它是 CoffeeScript,但如果您确实需要,应该很容易转换回 JavaScript。它假设您从服务器返回一些 json 作为 {url:"gae generated url"}

    $("#fileUploader").pluploadQueue
        runtimes : 'html5,html4'
        use_query_string : false
        max_file_size : '3mb'
        multipart: true
        unique_names : true
        multiple_queues : true
        filters : [{title : "Image files", extensions : "jpg,gif,png"}]
        preinit:
            UploadFile: (up, file) ->
                $.ajax
                    url: '/api/upload/url'
                    async: false
                    success: (data) ->
                        up.settings.url = data.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"}

    $("#fileUploader").pluploadQueue
        runtimes : 'html5,html4'
        use_query_string : false
        max_file_size : '3mb'
        multipart: true
        unique_names : true
        multiple_queues : true
        filters : [{title : "Image files", extensions : "jpg,gif,png"}]
        preinit:
            UploadFile: (up, file) ->
                $.ajax
                    url: '/api/upload/url'
                    async: false
                    success: (data) ->
                        up.settings.url = data.url
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文