GAE从Blobstore下载文件扩展名

5
我目前将一堆.docx文件存储在GAE Blobstore中。我最近发现这些文件在某些计算机上(例如Windows 7的IE 9)下载时没有文件扩展名,但在其他计算机上(如IE 8,Windows 7的Chrome)可以正常工作。
以下是文件在Blobstore中的存储方式:
f = files.blobstore.create(mime_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                           _blobinfo_uploaded_filename=filename)
## then some code to write data and save ##

以下是从Chrome检查器中获取的该文件的响应头信息:
Cache-Control:no-cache
Content-Disposition:attachment; filename="causes_of_ww1_emanresu"
Content-Length:12120
Content-Type:application/vnd.openxmlformats-officedocument.wordprocessingml.document
Date:Fri, 26 Oct 2012 23:54:09 GMT
Server:Google Frontend
X-AppEngine-Estimated-CPM-US-Dollars:$0.000033
X-AppEngine-Resource-Usage:ms=15 cpu_ms=0

这是我如何提供 Blob 服务的方法:
self.send_blob(blob_info, save_as=blob_info.filename, content_type=blob_info.content_type)

我甚至尝试硬编码content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document',但没有效果。

您有什么想法并且如何解决呢?


按照要求,在最初保存blob时,这是我获取文件信息的方式。我非常确定问题不是在这个层面上出现的,但以下是问题的前兆:

# get the file from a file_url with urlfetch
result = urlfetch.fetch(file_url)
headers = result.headers

# some custom functions to return a filename
username = self.get_username()
filename = get_filename(title, username)

# write the file to blobstore
f = files.blobstore.create(mime_type=headers['content-type'], 
                           _blobinfo_uploaded_filename=filename)
with files.open(f, 'a') as data:
    data.write(result.content)
files.finalize(f)
blob_key = files.blobstore.get_blob_key(f)

你是如何发送blob的?使用send_blob函数吗? - Stuart Langley
是的,我正在使用 self.send_blob(blob_info, save_as=blob_info.filename) - kennysong
你能否发布实际计算“filename”的代码,以确保它具有扩展名。此外,您可以使用BlobInfo检查文件名是否存储了正确的扩展名。 - Sebastian Kreft
当然,我刚刚添加了文件信息的源代码。而且我已经检查过BlobInfo,content_type是正确的。 - kennysong
我刚才意识到你在问什么了...不,filename 不包括文件扩展名('doc1' 而不是 'doc1.docx')。我假设 content_type 标头会处理这个问题(因为它在大多数浏览器上都可以正常工作)。这可能是问题的源头吗? - kennysong
2
你应该确保添加扩展名,而不是依赖浏览器为你完成。我们不会根据MIME类型为您执行此操作。 - Stuart Langley
1个回答

3

根据顶部的评论,解决方案是在BlobInfo中的文件名属性上添加文件扩展名。我最初没有意识到这是必要的,因为Chrome在下载时自动添加文件扩展名。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接