[FIXED] Downloading a file in downloads directory – flask, python


I’m using a flask app that has a button to download an HTML report. When you press the download button, an xlsx file will be created. No matter what os you are using python’s os module, your python code found the Downloads directory, so that app is fine until it runs on your local server was working.

The app has been deployed to the remote server. In this case, how do I tell the system the client download directory path where this xlsx file can be created? Or any other pointers to a better solution if I’m missing something?


To serve a user a file to download, the webserver needs to deliver it in a special way. You can’t use the os module to write to the user’s computer. It worked locally, because your server was the same computer as your user environment in the browser.

The correct way to serve a user a file for downloading is to use flask’s send_file method.

To show you the process, I’ve created an example below. In this example, I’m assuming that you have a function called build_report(...) which accepts a report_id and returns an io.BytesIO object containing the content of the xlsx file.

from flask import send_file

def download_report(report_id):
    file_obj = build_report(report_id)
    return send_file(

Answered By – damon

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published