0

By POST request I am going to let client store generated file to his local HHD by executing send_file() command. Here is the code:

def send_file_to():
    try:
        file_name = store_user_list()
        send_file(file_name, as_attachment=True, conditional=True)
        script_message = "File has been provided"
    except Exception as e:
        script_message = "File sending Error: " + str(e)
    return render_template('result.html', result_message=script_message)

unfortunately, it doesn't store file. Only if I change the script to:

       return send_file(file_name......

But I would like to get back on result.html page in any result. The command above leave me on the same page where I pressed the button "Download File".

Should I check any other event in @app to catch when download is finished?

Can you please help me understand the workflow that let me have required behaviour.

OcMaRUS
  • 329
  • 3
  • 13
  • https://flask.palletsprojects.com/en/2.0.x/api/#flask.send_file is used to return a file to the client, not to save a file at all – azro Dec 11 '21 at 19:22
  • @azro that exactly what I am trying to - return file to the client (store on his local hdd) – OcMaRUS Dec 11 '21 at 19:28
  • ow that is what you meant by "store a file", that isn't what the codes do, it just serve a content, then the client do what he want – azro Dec 11 '21 at 19:31
  • So I don't know how to do it, but for sure you need a `return send_file()` to give the user the file, then idk how to redirect just after – azro Dec 11 '21 at 19:32
  • @azro I updated my original question. I understand that I did something wrong and need advice how to fix it – OcMaRUS Dec 11 '21 at 19:33
  • You need to return the output of send_file in the try block or return the error in except block – snehilvj Dec 13 '21 at 17:46

0 Answers0