简体   繁体   English

使用Google App Engine Blobstore下载文件名

[英]Downloaded filename with Google App Engine Blobstore

I'm using the Google App Engine Blobstore to store a range of file types (PDF, XLS, etc) and am trying to find a mechanism by which the original filename of the uploaded file - as stored in blob_info - can be used to name the downloaded file ie so that the user sees 'some_file.pdf' in the save dialogue rather than 'very_long_db_key.pdf'. 我正在使用Google App Engine Blobstore来存储一系列文件类型(PDF,XLS等),并且我正在尝试找到一种机制,通过该机制可以使用上传文件的原始文件名(存储在blob_info中)来命名下载的文件,即用户在保存对话框中看到'some_file.pdf'而不是'very_long_db_key.pdf'。

I can't see anything in the docs that would allow this: 我在文档中看不到任何允许这样的内容:

http://code.google.com/appengine/docs/python/blobstore/overview.html http://code.google.com/appengine/docs/python/blobstore/overview.html

I've seen hints in other posts that you could use the information in blob_info to set the content-disposition header. 我在其他帖子中看到了一些提示,你可以使用blob_info中的信息来设置content-disposition头。 Is this the best approach to achieving the desired end? 这是实现理想目标的最佳方法吗?

There is an optional 'save_as' parameter in the send_blob function. send_blob函数中有一个可选的'save_as'参数。 By default this is set to False. 默认情况下,此值设置为False。 Setting it to True will cause the file to be treated as an attachment (ie it will trigger a 'Save/Open' download dialog) and the user will see the proper filename. 将其设置为True将导致文件被视为附件(即它将触发“保存/打开”下载对话框),用户将看到正确的文件名。

Example: 例:

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info,save_as=True)

It is also possible to overwrite the filename by passing in a string: 也可以通过传入一个字符串来覆盖文件名:

self.send_blob(blob_info,save_as='my_file.txt')

If you want some content (such as pdfs) to open rather than save you could use the content_type to determine the behavior: 如果您想要打开某些内容(例如pdfs)而不是保存,则可以使用content_type来确定行为:

blob_info = blobstore.BlobInfo.get(resource)
type = blob_info.content_type
if type == 'application/pdf':       
    self.response.headers['Content-Type'] = type
    self.send_blob(blob_info,save_as=False)
else:
    self.send_blob(blob_info,save_as=True)

Another option is to append the file name to the end of the download URL. 另一种选择是将文件名附加到下载URL的末尾。 For example: 例如:

/files/AMIfv95HJJY3F75v3lz2EeyvWIvGKxEcDagKtyDSgQSPWiMnE0C2iYTUxLZlFHs2XxnV_j1jdWmmKbSVwBj6lYT0-G_w5wENIdPKDULHqa8Q3E_uyeY1gFu02Iiw9xm523Rxk3LJnqHf9n8209t4sPEHhwVOKdDF2A/prezents-list.doc

If you use Jinja2 for templating, you can construct such an URL like this: 如果你使用Jinja2进行模板化,你可以构建这样的URL:

<a href="/files/{{blob_info.key()}}/{{blob_info.filename}}">{{file.filename}}</a>

then you should adapt your URL mapping accordingly to something like this: 那么你应该相应调整你的URL映射到这样的事情:

('/files/([^/]+)/?.*', DownloadHandler)

If you have the blob key in the URL, you can ignore the file name in your server-side code. 如果URL中包含blob密钥,则可以忽略服务器端代码中的文件名。

The benefit of this approach is that content types like images or PDF open directly in the browser, which is convenient for quick viewing. 这种方法的好处是可以在浏览器中直接打开图像或PDF等内容类型,便于快速查看。 Other content types will just be saved to disk. 其他内容类型将保存到磁盘。

For future reference, save_as and the BlobstoreDownloadHandler is documented here: 为了将来参考,save_as和BlobstoreDownloadHandler在此处记录:

http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html

It does seem like it should be a bit easier to find. 它似乎应该更容易找到。 Let's see if it can be improved. 让我们看看它是否可以改进。

Yes it is the best approach; 是的,这是最好的方法; just query the BlobInfo object using the given Blobstore key and use its content-type property. 只需使用给定的Blobstore密钥查询BlobInfo对象并使用其content-type属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM