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

SineCo
  • 33
  • 1
  • 2
  • 10
  • https://stackoverflow.com/questions/11620698/how-to-trigger-a-file-download-when-clicking-an-html-button-or-javascript?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Iron Fist Apr 16 '18 at 04:09

1 Answers1

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.

Rahul
  • 2,056
  • 1
  • 21
  • 37
  • 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 ? – SineCo Apr 16 '18 at 04:13
  • 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`. – Rahul Apr 17 '18 at 06:26