0

I have a .txt file on my directory. I just need a Button on my HTML page to download this file for the user.

I came up with this code, but the problem is when I click submit in my HTML page nothing happens and there are no errors. But the file is not being downloaded.

<form method="GET"> <input type="submit"> </form> 

My code in Flask

@app.route('/return-files', methods=['GET']) def return_file(): return send_from_directory(directory='Users/Desktop/Python/', filename='myFile.txt', as_attachment=True) 

Note: I checked all the questions on Stack overflow. Most of them put the file in the static folder before downloading or others who I did not understand what they are doing because they did not show what to do in HTML part

1

1 Answer 1

1

The part you are missing is form action.

 <form method="GET" action="{{ url_for('return_file') }}"> <input type="submit"> </form> 

if you will not specify this then submit button click have have no handler to handle it's action.

Also check for if your file path is correct or not if above changes doesn't work out.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sooooo much. I added what you mentioned, but I got this error. The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. The file path is correct 100% but the file is not in the static file, should I put it there ?
You can directly use action="/return-files" instead of url_for. I think some flask app setting is missing to make this magic function work url_for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.