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

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

问题描述

我想使用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 code IM是如下:

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]."');
}

请通过我的code和帮助我找到什么即时做错了。

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.

这是可能的工作是创建一个隐藏的iframe在HTML中,像这样的方法:

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

希望这有助于!

Hope this helps!

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

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