使用 Google App Engine Blobstore 下载的文件名
我正在使用 Google App Engine Blobstore 来存储一系列文件类型(PDF、XLS 等),并尝试找到一种机制,通过该机制可以使用上传文件的原始文件名(存储在 blob_info 中)来命名下载的文件即,以便用户在保存对话框中看到“some_file.pdf”而不是“very_long_db_key.pdf”。
我在文档中看不到任何允许这样做的内容:
http://code.google。 com/appengine/docs/python/blobstore/overview.html
我在其他帖子中看到过提示,您可以使用 blob_info 中的信息来设置内容处置标头。这是实现预期目标的最佳方法吗?
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 i.e. so that the user sees 'some_file.pdf' in the save dialogue rather than '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
I've seen hints in other posts that you could use the information in blob_info to set the content-disposition header. Is this the best approach to achieving the desired end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
send_blob 函数中有一个可选的“save_as”参数。默认情况下,该值设置为 False。将其设置为 True 将导致文件被视为附件(即,它将触发“保存/打开”下载对话框),并且用户将看到正确的文件名。
示例:
还可以通过传入字符串来覆盖文件名:
如果您希望打开而不是保存某些内容(例如 pdf),您可以使用 content_type 来确定行为:
There is an optional 'save_as' parameter in the send_blob function. By default this is set to 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.
Example:
It is also possible to overwrite the filename by passing in a string:
If you want some content (such as pdfs) to open rather than save you could use the content_type to determine the behavior:
为了便于将来参考,save_as 和 BlobstoreDownloadHandler 记录在此处:
http:// /code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html
看起来确实应该更容易找到。让我们看看是否可以改进。
For future reference, save_as and the BlobstoreDownloadHandler is documented here:
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.
另一种选择是将文件名附加到下载 URL 的末尾。例如:
如果您使用 Jinja2 进行模板化,则可以构造这样的 URL:
那么您应该相应地调整您的 URL 映射,如下所示:
如果 URL 中有 blob 键,则可以忽略您的 URL 中的文件名服务器端代码。
这种方式的好处是图片、PDF等内容类型直接在浏览器中打开,方便快速查看。其他内容类型将仅保存到磁盘。
Another option is to append the file name to the end of the download URL. For example:
If you use Jinja2 for templating, you can construct such an URL like this:
then you should adapt your URL mapping accordingly to something like this:
If you have the blob key in the URL, you can ignore the file name in your server-side code.
The benefit of this approach is that content types like images or PDF open directly in the browser, which is convenient for quick viewing. Other content types will just be saved to disk.
是的,这是最好的方法;只需使用给定的 Blobstore 键查询 BlobInfo 对象并使用其
content-type
属性即可。Yes it is the best approach; just query the BlobInfo object using the given Blobstore key and use its
content-type
property.