栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过Ajax和PHP强制下载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Ajax和PHP强制下载

您无法使用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或空白页中

<form target="..."

 function checkToken(token){    var $form = $("#downloadForm");    if ($form.length == 0) {        $form = $("<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($("<input>").attr({"value":args[field], "name":field}));    }    $form.submit();}

在script.php中,如果令牌正常,您需要立即从download.php执行代码,或者重定向到下载脚本:

header("Location: download.php?a=" . $filename)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号