I’ve been in the process of making my own website using html and flask. I want to make a download link on my html page. How do I do that?
Asked
Active
Viewed 402 times
-1
-
Next time, use Google to search for your question, and relevant answers on SO will appear at the top. The duplicate was the *first result* of a search for `flask download file`. Also, while you are composing your question, a list entitled **Questions that may already have your answer** shows up with potentially relevant questions. **Use that list** and open up the suggested questions in new tabs. This site has been around since 2008; if you suspect a question has already been asked, it probably has. Duplicates like this just waste time and effort. – MattDMo Jun 11 '21 at 15:57
1 Answers
0
main.py
from flask import Flask, render_template, send_file
app = Flask(__name__)
@app.route(‘/’)
def home():
return render_template(‘index.html’)
@app.route(‘/download’)
def download_file():
p = “app.exe”
return send_file(p,as_attachment=True)
if __name__ == “__main__”:
app.run()
index.html
<!DOCTYPE html>
<h1>Download Link</h1>
<p>
<a href=“{{url_for(‘download_file’)}}”>Download</a>
</p>
It is IMPORTANT that app.exe is in the same file location as main.py!

NewToPython
- 97
- 3
- 9
-
And no, `app.exe` or whatever file you're downloading can be anywhere - you just need to supply a fully-qualified path. See the answers in the dupe for more information. – MattDMo Jun 11 '21 at 15:58