我的包含send_file()的函数似乎没有更新 [英] My function containing send_file() does not seem to update

查看:51
本文介绍了我的包含send_file()的函数似乎没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask应用程序更新一些PDF文件,将它们转换为Excel文件,然后将此文件发送回用户.我正在使用一个实例文件夹来存储pdf和excel文件.

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.

但是,当用户按下下载"按钮以下载生成的Excel文件时,则会下载旧文件(从旧会话中下载).

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).

此外,例如,当我尝试更改代码时,我更改了此Excel文件的名称:我可以在实例文件夹中看到新名称,但是当我使用webapp下载文件时,它仍然是旧名称(旧文件).我不知道webapp在哪里寻找这个旧文件...

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>

总体而言,我真的是webapp的新手,谢谢您的帮助:)

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

推荐答案

send_file 采用 cache_timeout 参数,该参数是您要缓存下载内容的秒数.默认为12小时.

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/

这篇关于我的包含send_file()的函数似乎没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆