AaronDT
AaronDT

Reputation: 4050

Download file from root directory using flask

I am generating a xlsx file which I would like to download once it is created. The file is created using a module called 'xlsxwriter'. It saves the file in my root directory, however I cant figure out how to access it via flask, so that it starts a download.

This is how I create the file:

workbook = xlsxwriter.Workbook('images.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(..someData..)

It saves the file in my root directory.

Now I am trying to access it in order to download it via flask:

app = Flask(__name__, static_url_path='')

@app.route('/download')
def download():
    # do some stuff
    return Response(
    app.send_static_file('images.xlsx'),
    mimetype="xlsx",
    headers={"Content-disposition":
             "attachment; filename=images.xlsx"})

However, I get a 404 Error. Is using send_static_file the correct way to go here?

Upvotes: 1

Views: 783

Answers (1)

AaronDT
AaronDT

Reputation: 4050

I found a solution using 'send_file' instead. Providing the path to my file like so:

from flask import send_file

return send_file(pathToMyFile, as_attachment=True)

Upvotes: 2

Related Questions