使用 Jquery、PHP 下载 Ajax 文件 [英] Ajax File Download using Jquery, PHP

查看:76
本文介绍了使用 Jquery、PHP 下载 Ajax 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ajax 功能进行下载,用户将单击下载链接(使用 ajax 和 $_GET)访问 PHP 文件,该文件将处理发送的 $_GET 变量并访问正确的文件进行下载.

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.

我有几个 PHP 脚本来处理 $_GET 变量的处理,它们自己工作,但是当使用 Ajax 访问时,它们停止工作.

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.

我使用的 Ajax/PHP 代码如下:

The Ajax/PHP code im using is below:

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.

谢谢

推荐答案

我认为问题在于您正尝试将文件结果加载到 #downloadmsg 中,这不会起作用,因为 .load() 只是将结果加载为 HTML...不是二进制数据或其他编码.

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.

一种可行的方法是在 HTML 中创建一个隐藏的 iframe,如下所示:

One approach that might work is creating a hidden iframe in HTML, like this:

<iframe id="secretIFrame" src="" style="display:none; visibility:hidden;"></iframe>

然后,将 iframe 的 attr 设置为您的查询字符串:

Then, set the attr of the iframe to your querystring:

$("#secretIFrame").attr("src","myphpscript.php?option1=apple&option2=orange");

然后在设置源时使用 PHP 标头强制下载(这是从我的一个使用八位字节流的脚本中设置的导出器标头的示例):

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):

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 ");

希望这有帮助!

这篇关于使用 Jquery、PHP 下载 Ajax 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆