使用Jquery、PHP下载Ajax文件

时间:2020-12-21 09:45:31

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:

im使用的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

谢谢

2 个解决方案

#1


55  

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设置为querystring:

$("#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!

希望这可以帮助!

#2


6  

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请求就能完成!使用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

像咒语一样:哦

#1


55  

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设置为querystring:

$("#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!

希望这可以帮助!

#2


6  

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请求就能完成!使用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

像咒语一样:哦