简体   繁体   中英

Python Flask - How to download a file with send_from_directory AND display text output in browser

I'd like to be able to display a message to the user after a download is triggered via send_from_directory.

@app.route('/downloadFile', methods=['GET'])
def download():

    return send_from_directory(output_directory, "myfile.txt", as_attachment=True)

This will trigger a download after the /downloadFile endpoint is visited, but in addition to the download being triggered, I'd like to have output in the browser that says something like "file download triggered". Is there a way to achieve this? If I wasn't using send_from_directory, I could do something like this to return some JSON return {'Status': 'file download triggered'}

I'm looking for a way to combine the two, that way the user doesn't see a blank screen when they hit this endpoint.

What you could do is, return render_template() at the given endpoint to render an html file which would display something like "your file is now being downloaded" or whatever message you want and in the same html file, use a JS to trigger a 2nd endpoint which would download the file.

@app.route('/downloadPage', methods=['GET'])
def download_page():
    return render_template("download_page.html")

@app.route('/downloadFile')
def download_file():
    return send_from_directory(output_directory, "myfile.txt", as_attachment=True)

In the HTML file, the JS will be something like -

<SCRIPT>
   window.location.href = "/DownloadFile";
</SCRIPT>

EDIT 1:

@fpolig01 Here is what I did -> I used save_file from flask created a function which returns this

def download_file():
    return send_file('static/downloads/filename.csv',
                     mimetype='text/csv',
                     as_attachment=True)

After this, in your app.route() code, call this function. something like ->

@app.route('/downloadFile')
def download_file():
    result = download_file()
    return result

In the JS, you will have to redirect to this URI, it will directly start the download/will display a pop up to save the file. the client still stays on the last page only. At least this works for me.

Or the least you could do is, create an html page with a download your file button, configure the button to go to this download link and then that would work.

Update me with what you do

For any request, there can only be a single response. The HTML page response and the file download response must be their own respective responses to two separate requests.

In order to get the effect you want, you would want two endpoints: one endpoint to return an HTML response, and a second endpoint to handle the file download.

Your HTML response from your first route can contain javascript code to initiate the file download from the second endpoint. An implementation of this might look something close to pushpak ruhil's answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM