我正在使用 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 http://code.google.com/appengine/docs/python/blobstore/overview.html
我在其他帖子中看到过提示,您可以使用 blob_info 中的信息来设置内容处置标头。这是实现预期目标的最佳方法吗?
send_blob 函数中有一个可选的“save_as”参数。默认情况下,该值设置为 False。将其设置为 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)
也可以通过传入字符串来覆盖文件名:
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)