Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax File Download using Jquery, PHP

Tags:

jquery

ajax

php

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.

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.

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.

Thanx

like image 243
Stanley Ngumo Avatar asked Aug 30 '10 10:08

Stanley Ngumo


People also ask

How to download AJAX response file?

Downloading PDF File on Button Click using jQueryInside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the jQuery AJAX function. Inside the jQuery AJAX function, using the XmlHttpRequest (XHR) call, the PDF file is downloaded as Byte Array (Binary Data).

How do I download a CSV file from Ajax in PHP?

ajax({ url: 'exportCSV. php', type: 'post', dataType: 'html', data: { Year: $('input[name="exportYear"]'). val() }, success: function(data) { var result = data console. log(result); } }); });


2 Answers

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.

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

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

Then, set the attr of the iframe to your 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):

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!

like image 93
Trafalmadorian Avatar answered Sep 19 '22 15:09

Trafalmadorian


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

<?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

function download(filename){     window.location="http://whateveryoursiteis.com/download.php?file="+filename; } 

Works like a charm :O

like image 26
ksealey Avatar answered Sep 20 '22 15:09

ksealey