通过Ajax和PHP强制下载

我想创建一个下载脚本,允许强制下载JPG。 这是我的PHP脚本:

 

这是我的js代码的代码段:

 function downloadFile(a){ document.location = "download.php?a="+ a; } 

使用此代码示例没有任何反应。 如果我将结果附加到HTML标记中,它会显示文件的内容。

任何想法如何教浏览器下载此文件?

编辑:脚本更新

您无法使用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; }}); }) 

将被浏览器阻止。 因此,如果您想通过post传递一些数据,您可以使用

将表单提交到隐藏的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中,如果令牌为Ok,则需要立即执行download.php中的代码,或者重定向下载脚本:

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

将mime类型设置为image/jpeg很可能不起作用。 因此,您需要application/octet-stream来强制下载。

用以下内容替换php中的内容类型标题:

 header('Content-Type: application/octet-stream'); 

此外,一个不错的解决方案,而不是使用document.location是注入一个iframe。 在成功回调中使用以下function

 function downloadFile(url) { var iframe; iframe = document.getElementById("download-container"); if (iframe === null) { iframe = document.createElement('iframe'); iframe.id = "download-container"; iframe.style.visibility = 'hidden'; document.body.appendChild(iframe); } iframe.src = url; } 

您的脚本中似乎有错误。 首先,GET变量的正确拼写是$ _GET [‘a’],而不是$ GET [‘a’]。 这里的第二个问题是你有额外的左括号,当我复制你的代码时,我收到500内部服务器错误响应。 如果我们纠正错误,它似乎工作正常。 只需尝试修正您的代码版本。

  

你有点困惑。 正如FAngel指出的那样,你无法通过AJAX下载文件。 您需要做的是将用户重定向到另一个页面,然后在其中包含您的上述PHP代码。 那么PHP代码应该允许用户直接下载文件。 你正在尝试的是绝对可能的,你只需要从另一个方向接近它,即不是用AJAX。

您可以使用鼠标中间事件强制下载文件:

 const url = "http://sofzh.miximages.com/php/googlelogo_color_120x44dp.png"; const forceDownload = url => { try { const link = document.createElement('a'); const fileName = url.substring(url.lastIndexOf('/') + 1, url.length); const event = new MouseEvent( "click", { "button": 1, "which": 1 }); link.href = url; link.download = fileName; link.dispatchEvent(event); } catch(e) { document.location = url; } } forceDownload(url);