Possible Duplicate:
How to set file name in response
I store files in MongoDB. To serve a file from Pyramid, I do this:
# 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
Using this method, the filename defaults to the ObjectId
string of the file, which isn't pretty and lacks the correct file extension. I've looked in the docs to see how/where I can rename the file inside the Response
object, but I can't see it. Any help would be fantastic.