koteikin
5/30/2018 - 3:53 PM

How to have Flask download a file and then serve it as an attachment

How to have Flask download a file and then serve it as an attachment

@app.route('/download/', methods=['GET'])
def download():
    url = request.args['url']
    filename = request.args.get('filename', 'image.png')
    r = requests.get(url)
    strIO = StringIO.StringIO(r.content)
    return send_file(strIO, as_attachment=True, attachment_filename=filename)