使用 Google App Engine Blobstore 下载的文件名

发布于 2024-12-03 03:36:54 字数 430 浏览 3 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

紫南 2024-12-10 03:36:54

send_blob 函数中有一个可选的“save_as”参数。默认情况下,该值设置为 False。将其设置为 True 将导致文件被视为附件(即,它将触发“保存/打开”下载对话框),并且用户将看到正确的文件名。

示例:

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)

还可以通过传入字符串来覆盖文件名:

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

如果您希望打开而不是保存某些内容(例如 pdf),您可以使用 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)

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:

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:

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)
哥,最终变帅啦 2024-12-10 03:36:54

为了便于将来参考,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.

恬淡成诗 2024-12-10 03:36:54

另一种选择是将文件名附加到下载 URL 的末尾。例如:

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

如果您使用 Jinja2 进行模板化,则可以构造这样的 URL:

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

那么您应该相应地调整您的 URL 映射,如下所示:

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

如果 URL 中有 blob 键,则可以忽略您的 URL 中的文件名服务器端代码。

这种方式的好处是图片、PDF等内容类型直接在浏览器中打开,方便快速查看。其他内容类型将仅保存到磁盘。

Another option is to append the file name to the end of the download 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:

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

then you should adapt your URL mapping accordingly to something like this:

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

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.

小巷里的女流氓 2024-12-10 03:36:54

是的,这是最好的方法;只需使用给定的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文