我有一个 django 响应,向用户发送一个文件,然后浏览器会忽略该文件。我如何打开文件保存对话框来保存它?
这是我的 Django 代码:
mimetype = "application/x-unknown"
file = somefilefromthedatabase
response = HttpResponse(file, mimetype=mimetype)
response["Content-Disposition"]= "attachment; filename=%s" % os.path.split(file.name)[1]
return response
当我访问直接生成此 View 的 View 时,它将文件保存到数据库中。当通过 jquery 访问它时,没有任何反应。 js代码在这里:
$(".jp-download").click(function(){
current=$("#download").data('current').find('.id').text();
$.post('/actions/',{action:'download',item:current});
});
最佳答案
为了下载它,您需要传递正确的 header ,并将实际页面位置设置为新地址,就像您正确更改页面一样。
我认为您无法通过 AJAX 调用启动下载。如果开始下载,它无论如何都不会更改页面。
要么将参数作为 GET 调用,要么创建表单并提交。
window.location = '/actions/?action=download&item=' + current;
关于jquery - 如何使用 jQuery 保存文件响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5135634/