My function containing send_file() does no

2019-08-17 10:07发布

I am using a Flask application to update some PDF files, convert them to an Excel file and send this file back to the user. I am using an instance folder to store the pdf and the excel files.

But when the user press the button "Download" in order to download the generated Excel file, an old file is downloaded (from an older session).

Moreover, when I try to change my code, for example, I changed the name of this Excel file: I can see the new name in the instance folder, but when I download the file with the webapp, it is still the old name (and old file). I have no idea where the webapp is looking for this old file...

MEDIA_FOLDER = '/media/htmlfi/'
app = Flask(__name__)
app.config.from_object(Config)
INSTANCE_FOLDER = app.instance_path

app.config['UPLOAD_FOLDER'] = INSTANCE_FOLDER+MEDIA_FOLDER
@app.route('/file/')
def send():
    folder = app.config['UPLOAD_FOLDER']
    try:
        return send_file(folder+ "file.xlsx", as_attachment=True)
    finally:
        os.remove(folder+ "file.xlsx")
<a href="{{ url_for('send') }}"  ><button class='btn btn-default'>DOWNLOAD</button></a>

I am really new to webapp in general, thank you for your help :)

1条回答
狗以群分
2楼-- · 2019-08-17 10:29

send_file takes a cache_timeout parameter which is the number of seconds you want to cache the download. By default is 12 hours.

return send_file(
    file.file_path(),
    as_attachment=True,
    cache_timeout=app.config['FILE_DOWNLOAD_CACHE_TIMEOUT'],
    attachment_filename=file.file_name
)

http://flask.pocoo.org/docs/1.0/api/

查看更多
登录 后发表回答