0

How do I download files from my gridFS store in Mongodb?

I have the following code:

@bottle.route('/download')
def download():
    file_id = ObjectId(bottle.request.query.id)
    if file_id:
        try:
            file_to_download = fs.get(file_id)
        except:
            return "document id not found for id:" + file_id, sys.exc_info()[0]
        return (file_to_download)
    return bottle.template('files_test/download')

which returns gibberish in the browser. How do I specify that I want it to be downloaded?

Edit:

This is the hyperlink I want to use to download the file:

<a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>

Have I missed it here well?

Jeff
  • 4,285
  • 15
  • 63
  • 115

1 Answers1

0

See this question:

flask return image created from database

You need to set the Content-Type and Content-Disposition headers so the browser knows what type of file you're sending, and that it should be downloaded rather than displayed.

Community
  • 1
  • 1
A. Jesse Jiryu Davis
  • 23,641
  • 4
  • 57
  • 70
  • It almost worked, but when I tested with an xlsx file, it errored out "Excel cannot open the file because the file format or extension is not correct." I'll try again with a txt file? – Jeff Feb 18 '14 at 18:49