小编典典
您无法使用Ajax下载文件。因此,如果您在ajax上发生了某些情况,则应返回url作为响应,并像document.location =
"url"开始下载过程一样应用它。
这里有一个音符。我记得,如果不是用户单击启动浏览器,浏览器将阻止文件下载。因此,这将正常工作:
.click(function(){
document.location = "download url"
})
但是,如果不是通过用户单击启动它,它将被阻止。因此,这样的代码:
.click(function(){
$.ajax({...,
success:function(download_url_from_server){
document.location = download_url_from_server;
}});
})
将被浏览器阻止。因此,如果您想在发布时传递一些数据,可以使用以下方法将表单提交到隐藏的iframe或空白页中
function checkToken(token){
var $form = $("#downloadForm");
if ($form.length == 0) {
$form = $("
").attr({ "target": "_blank", "id": "downloadForm", "method": "POST", "action": "script.php" }).hide();$("body").append($form);
}
$form.find("input").remove();
var args = { a: "checkToken", b: token }
for (var field in args) {
$form.append($("").attr({"value":args[field], "name":field}));
}
$form.submit();
}
在script.php中,如果令牌正常,您需要立即从download.php执行代码,或者重定向到下载脚本:
header("Location: download.php?a=" . $filename)
2020-07-26