Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top