Python 使用flask,再次发送_文件以获得具有相同路径和名称缓存问题的更改文件?

Python 使用flask,再次发送_文件以获得具有相同路径和名称缓存问题的更改文件?,python,flask,Python,Flask,在我们的flask网站上,我们实现了一个“下载”按钮。此按钮调用flask中的函数,该函数通过send_file()向用户提供压缩文件。第一次一切正常。第二次单击按钮后,用户再次收到完全相同的文件。看起来不错,但是当再次单击按钮时,flask应该发送的文件被更改,旧文件不再存在 我试图理解为什么PyCharm调试器会出现这种情况,并注意到,第二次按下下载按钮后,下载例程甚至不再被调用。因此,我认为在后台正在进行某种缓存。我甚至从文件系统中删除了要发送的文件,然后第二次按下下载按钮。。。我把原始文

在我们的flask网站上,我们实现了一个“下载”按钮。此按钮调用flask中的函数,该函数通过send_file()向用户提供压缩文件。第一次一切正常。第二次单击按钮后,用户再次收到完全相同的文件。看起来不错,但是当再次单击按钮时,flask应该发送的文件被更改,旧文件不再存在

我试图理解为什么PyCharm调试器会出现这种情况,并注意到,第二次按下下载按钮后,下载例程甚至不再被调用。因此,我认为在后台正在进行某种缓存。我甚至从文件系统中删除了要发送的文件,然后第二次按下下载按钮。。。我把原始文件拿回来了

也许你们也经历过类似的事情,可以用你们的知识来帮助我。先谢谢你

下载代码:

@app.route('/download/<expID>')
def download(expID):

 experiment = dbc.getExperiment(int(expID))

# only execute, if owner of experiment calls it AND experiment is NOT running or in queue
 if experiment.getOwner() == cas.username and experiment.getStatus() != 1 and experiment.getStatus() != 2:
    ownerPath = dbc.getUser(experiment.getOwner()).getPath()
    fileName = cas.username + "_" + expID

    # remove old zip (if existing)
    try:
        os.remove(experiment.getPath() + "/" + fileName + ".zip")
    except FileNotFoundError:
        pass

    # create zip in owner folder to work around recursion problem
    make_archive(ownerPath + "/" + fileName, 'zip', root_dir=experiment.getPath(), base_dir=None)

    # move from owner folder to owner/experiment folder
    shutil.move(ownerPath + "/" + fileName + ".zip", experiment.getPath() + "/" + fileName + ".zip");

    # trigger download
    return send_file(
        experiment.getPath()+"/"+fileName + ".zip", as_attachment=True)
 return redirect('/dashboard')
@app.route(“/download/”)
def下载(expID):
experience=dbc.getExperiment(int(expID))
#仅当实验的所有者调用它并且实验未运行或未在队列中时才执行
如果experience.getOwner()==cas.username和experience.getStatus()!=1和实验。getStatus()!=2:
ownerPath=dbc.getUser(experiment.getOwner()).getPath()
fileName=cas.username+“\u”+expID
#移除旧拉链(如果存在)
尝试:
删除(experiment.getPath()+“/”+文件名+“.zip”)
除FileNotFoundError外:
通过
#在所有者文件夹中创建zip以解决递归问题
制作归档文件(所有者路径+“/”+文件名,'zip',根目录=experience.getPath(),基目录=None)
#从所有者文件夹移动到所有者/实验文件夹
move(ownerPath++“/”+fileName++.zip”,experience.getPath()++“/”+fileName++.zip”);
#触发器下载
返回发送文件(
experiment.getPath()+“/”+文件名+“.zip”,如_attachment=True)
返回重定向(“/dashboard”)
按钮:

<form action="/download/{{experiments[i][0]}}">
                <input type="submit" id="download" value="download" />
            </form>

我在send\u文件的文档中找到了一个解决方案。 Flask确实会缓存您通过send_file发送的文件,并且在一定时间内不会访问您之前调用的函数。要限制此行为,可以使用cache\u timeout参数。对我来说,它看起来像这样:

return send_file(
            experiment.getPath()+"/"+fileName + ".zip", as_attachment=True, cache_timeout=0)

出于好奇,您正在处理的ZIP文件有多大?还有什么
make_archive
功能,我们可以看到吗?@v25压缩文件有几个字节大-没有什么特别的。make_存档是从shutil库导入的-