我的包含send_file()的函数似乎没有更新

问题描述 投票:0回答:1

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

但是当用户按下“下载”按钮以下载生成的Excel文件时,将下载旧文件(来自较旧的会话)。

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

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一般都是新手,谢谢你的帮助:)

python file flask upload
1个回答
0
投票

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

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/

© www.soinside.com 2019 - 2024. All rights reserved.