使用Google App Engine Blobstore下载的文件名 [英] Downloaded filename with 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中的信息来设置内容处置标题。这是达到理想目的的最佳方法吗?
send_blob函数中有一个可选的'save_as'参数。默认情况下,它被设置为False。将其设置为True将导致文件被视为附件(即它会触发'保存/打开'下载对话框),用户将看到正确的文件名。
<例如:
pre code> class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self,resource):
resource = str(urllib.unquote(资源))
blob_info = blobstore.BlobInfo.get(资源)
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(资源)
类型= blob_info.content_type
如果输入=='application / pdf':
self.response .headers ['Content-Type'] =类型
self.send_blob(blob_info,save_as = False)
else:
self.send_blob(blob_info,save_as = True)
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?
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)
这篇关于使用Google App Engine Blobstore下载的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!