1

In Flask I want the user to be able to download a file from a S3 bucket using Boto. Of course if Flask downloads something from S3 it stores the file on the server. However I want the the file to be downloaded to the users machine if they click a download button. Is that possible? My idea was the following. When the download button gets clicked Flask fetches the file from S3 and stores it on the server afterwards the users machine downloads the file. Afterwards the file on the server gets deleted. Please tell me if there is a better way. If I do it like this it works. Unfortunately I need to render my dashboard again after the file was downloaded so I can't return the file.

Flask:

@app.route('/download')
@login_required
def dowloadfile():
    try:
        #Boto3 downloading the file file.csv here
        return send_file('file.csv', attachment_filename='file.csv')
    except Exception as ermsg:
        print(ermsg)
        return render_template('dashboard.html', name=current_user.username, ermsg=ermsg)

HTML:

<a href="./download" style="position:absolute; margin-left:10px; margin-top:5px; margin-bottom:10px;"><button class="buttonstyle" onclick="showImage();">Download</button></a>

The problem is that when the download button is clicked a full screen image is shown which is my loading screen. This loading screen disappears when the function is done and a new html is rendered. With the code above the loading screen appears and stays forever even when the file is aready downloaded to the users machine. How could you fix that?

jz22
  • 2,328
  • 5
  • 31
  • 50
  • Possible duplicate of [Downloading a file from an s3 Bucket to the USERS computer](https://stackoverflow.com/questions/43215889/downloading-a-file-from-an-s3-bucket-to-the-users-computer) – Allie Fitter Sep 15 '17 at 20:38
  • Check out my answer to this [question](https://stackoverflow.com/a/43218719/5854907). – Allie Fitter Sep 15 '17 at 20:38

0 Answers0