My function containing send_file() does not seem to update

一笑奈何 提交于 2019-12-12 01:01:53

问题


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:


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/



来源:https://stackoverflow.com/questions/55588970/my-function-containing-send-file-does-not-seem-to-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!