【发布时间】:2012-10-16 04:25:30
【问题描述】:
我将文件存储在 MongoDB 中。为了从 Pyramid 提供文件,我这样做:
# view file
def file(request):
id = ObjectId(request.matchdict['_id'])
collection = request.matchdict['collection']
fs = GridFS(db, collection)
f = fs.get(id)
filename, ext = os.path.splitext(f.name)
ext = ext.strip('.')
if ext in ['pdf','jpg']:
response = Response(content_type='application/%s' % ext)
else:
response = Response(content_type='application/file')
response.app_iter = FileIter(f)
return response
使用这种方法,文件名默认为文件的ObjectId 字符串,它不漂亮并且缺少正确的文件扩展名。我查看了文档以了解如何/在哪里可以重命名 Response 对象内的文件,但我看不到它。任何帮助都会很棒。
【问题讨论】: