1. 基础框架,
后端:python3.6 + flask + flask-restful
前端:vue
2. 场景:有一个功能界面需要上传下载文件,且支持更改新的上传文件,
文件上传:通过request.files 获取到文件流,os.makedirs创建文件存储目录, file.save(filepath)保存,另外保存一条文件上传记录,涵盖文件名,文件保存路径等等。一切都ok
文件下载: 通过前端界面获取到当前记录的id,通过id 查找文件信息,找到 文件目录filepath,
response = make_response(send_from_directory(filepath, fullflname, as_attachment=True))
获取到response对象,返回文件流。
3. 问题:当修改新的上传文件为B时,点击下载 还是获取的之前文件-A,清理浏览器缓存后,点击下载,文件为B,正确。
4. 方案,经过一系列资料查询,得知,google 会对文件下载进行缓存的。具体可详查,
解决方法: 在下载获取到response对象后,增加headers 设置 cache-controll = “no-store“
response.headers["Cache-Control"] = "no-store"
问题解决。
关于cache-controll的介绍:https://blog.csdn.net/u012375924/article/details/82806617