在和我意识到,也许我的问题不是100%清楚。无论如何,我想分享我提出的解决方案。我在Flask应用程序中创建了两个结束点:
第一个通过AJAX POST从客户端获取数据,并将它们临时存储在Redis中(我已经有一个Redis实例用于缓存)并为该文件生成一个UUID。
@mod.route("/create-csv", methods=['POST'])
def create_csv():
csv_string = request.form.get('csv')
file_id = str(uuid())
rstore.setex(file_id, 60, csv_string)
return jsonify({}), 202, {'Location': url_for('api.download',
file_id=file_id,
_external=True,
_scheme='https')}
第二个端点只是将文件发送到具有适当标头的客户端。
@mod.route("/download/<file_id>", methods=['GET'])
def download(file_id):
file_content = rstore.get(file_id)
response = make_response(file_content)
response.headers["Content-Disposition"] = "attachment; filename=keywords.csv"
response.headers['Content-Type'] = "application/octet-stream"
return response
在客户现场,我有以下的JavaScript代码:
self.save = function(csvdata) {
$.post("/api/create-csv", csvdata, function(data, status, response){
var file_url = response.getResponseHeader('Location');
window.location.assign(file_url);
});
}
所以当POST请求发送成功我只是分配给当前URL的文件下载的URL。
download.js作者:如果有人知道Safari浏览器修复,我会非常感激。也就是说,如果您使用服务器生成内容,则只需使用内容处置标头即可在隐藏的iframe中触发下载。 – dandavis
有一个很好的剪辑顶部,将告诉你如何下载表单响应或iframe位置更改:http://php.net/manual/en/function.header.php#refsect1-function.header-examples – dandavis