使用 Jquery、PHP 下载 Ajax 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3599670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax File Download using Jquery, PHP
提问by Stanley Ngumo
I want to use the ajax functionality to download whereby the user will click the download link which will (using ajax and $_GET) access a PHP file which will process the sent $_GET variables and access the correct file for downloading.
我想使用 ajax 功能进行下载,用户将单击下载链接(使用 ajax 和 $_GET)访问一个 PHP 文件,该文件将处理发送的 $_GET 变量并访问正确的文件进行下载。
I have a few PHP scripts to handle the processing of the $_GET variables which work on their own but when accessed using Ajax, they stop working.
我有几个 PHP 脚本来处理 $_GET 变量的处理,这些变量独立工作,但是当使用 Ajax 访问时,它们停止工作。
The Ajax/PHP code im using is below:
我使用的 Ajax/PHP 代码如下:
function ajaxDown(){
$('#downloadmsg').html(
'<img src=\"media/images/ajaxloader.gif\" width=\"128\" height=\"15\">');
$('#downloadmsg').load(
'media/downloads/downManager.php?file=".$filequery['filename']."&ftype=".$downex[1]."');
}
Please look through my code and help me find what Im doing wrong.
请查看我的代码并帮助我找出我做错了什么。
Thanx
谢谢
回答by Trafalmadorian
I think the problem is that you're trying to load a file result INTO #downloadmsg, which isn't going to work because .load() is only going to load results as HTML...NOT binary data or other encoding.
我认为问题在于您试图将文件结果加载到 #downloadmsg 中,这将不起作用,因为 .load() 只会将结果加载为 HTML...而不是二进制数据或其他编码。
One approach that might work is creating a hidden iframe in HTML, like this:
一种可行的方法是在 HTML 中创建一个隐藏的 iframe,如下所示:
<iframe id="secretIFrame" src="" style="display:none; visibility:hidden;"></iframe>
Then, set the attr of the iframe to your querystring:
然后,将 iframe 的 attr 设置为您的查询字符串:
$("#secretIFrame").attr("src","myphpscript.php?option1=apple&option2=orange");
and then using PHP headers to force the download when the source is set (here's an example of an exporter header set from one of my scripts that uses an octet stream):
然后在设置源时使用 PHP 标头强制下载(这是从我的一个使用八位字节流的脚本中设置的导出器标头的示例):
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=data.xls ");
header("Content-Transfer-Encoding: binary ");
Hope this helps!
希望这可以帮助!
回答by ksealey
I know I'm late! But I think I have a solution that's a little cleaner without the use of a hidden iframe and you won't even need an ajax request to do it! Using PHP Headers as noted in the accepted answer in a download.php file
我知道我迟到了!但我认为我有一个更简洁的解决方案,无需使用隐藏的 iframe,您甚至不需要 ajax 请求来执行此操作!使用 download.php 文件中接受的答案中所述的 PHP 标头
<?php
//download.php
/*
All your verification code will go here
*/
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=".$_GET['file']);
header("Content-Transfer-Encoding: binary ");
And on the JS end, simply
而在 JS 端,简单地
function download(filename){
window.location="http://whateveryoursiteis.com/download.php?file="+filename;
}
Works like a charm :O
像魅力一样工作:O