Lucifer N.
Lucifer N.

Reputation: 1056

Flask - action when HTML button is pressed?

I am trying to make dynamically generated download pages for some uploaded files on my site. I've got routing set up and a template that will show the filename. I want to have a button on this page, that when pressed will call send_from_directory and download the file. How can I do this?

My function in python that renders the download page:

@app.route('/<new_folder_name>/', subdomain='f')
def uploaded_file(new_folder_name):
    filename = subfolder_fetch(new_folder_name)
    return render_template("download.html", filename=filename)

My button in html is just something like:

{% block body %}
<div id = "filename">
{{filename}}
</div>

<button name="dlbutton">Download</button>


{% endblock %}

I don't have a form or anything set up, do I need to do something like set one up and then catch the request with flask? How would I do this, or is there a simpler way?

Upvotes: 0

Views: 2384

Answers (1)

iurisilvio
iurisilvio

Reputation: 4987

Create a download_file route with your send_from_directory response and use a link to download your file:

<a href="{{ url_for('download_file', filename=filename) }}" name="dlbutton">Download</a>

Upvotes: 2

Related Questions