Python Forum
Redirect upon submit button linking to send_file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect upon submit button linking to send_file
#2
Disappointed that nobody was able to help however, I've figured this out and I hope it helps somebody in my position in the future - sorry if the formatting is a touch messy, it's been a long day, but will tidy up if required!

The problem I found is that when pressing download and using the route with the send_file function on as the href, the send_file kicked in, but not the template.

So, do this!...

1. HTML Page and Flask route with your download button (please note, your file will exist on the server at this point, i.e I generated mine on generate.html and then sent the user to here, passing over the file location to this Flask route) - for this example, lets say download.html.

2. HTML Page and Flask route you want to display after downloading the file, i.e Thanks for your download - for this example, let's say thankyou.html

3. Flask route with your send_file function. (HTML page not needed as it wont render after the send_file is called.) - let's call this flask route grab_file

On download.html

<a href='download' target='_self' type='button'>
  <button class='btn btn-default'>Download!</button>
</a>
Flask route for download.html located in (for example) your app.py

@app.route('/download/', methods=['GET'])
def download():
        filename = download_video.session_id

        return render_template('download.html') 
On thankyou.html

Within the body tags.

<script>

  $(document).ready(function() {
    window.location.replace("/grab_file/");
    // Code to run as soon as the page is ready
  })

 </script>
Don't forget to call jquery between the head tags.

<head>

  <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'>

</script>
</head>
...and on your route for thankyou.html

@app.route('/thankyou/', methods=['GET'])
def thankyou():


    return render_template('thankyou.html')
On your grab_file route - this is the route we're calling in the jquery over on thankyou.html...

@app.route('/grab_file/', methods=['GET', 'POST'])
def grab_file():

    return send_file(CHECK THE DOCS FOR ATTRIBUTES HERE but you need as_attachment=True)
Reply


Messages In This Thread
RE: Redirect upon submit button linking to send_file - by Jamesunsworth - Nov-10-2020, 08:50 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020