使用响应包在 Safari 上下载文件:加载资源失败:帧加载中断

File download on Safari using response package: Failed to load resource: Frame load interrupted

我想在用户单击按钮时下载文件。我通过 python flask 实现了它,响应包如下

bib_text = 'test'

filename = 'test_file'
response = make_response(bib_text)
response.headers["Content-Disposition"] = "attachment; filename=%s.bib" % filename
return response

上面的代码在 Chrome 上运行良好,但在 Safari 上,javascript 控制台告诉我错误

Failed to load resource: Frame load interrupted

它下载的不是 .bib 文件,而是 .bib.html 文件? 知道发生了什么事吗?是否有另一种适用于 Safari 和 Chrome 的方法?

添加以下header规范即可解决问题

response.headers["Cache-Control"] = "must-revalidate"
response.headers["Pragma"] = "must-revalidate"
response.headers["Content-type"] = "application/bib"

Content-type 规范很重要