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
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!
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
Related
I'm working on a Yii2 rest Api that's connected to AngularJS front end.
I'm trying to build an actionDownloadAsExcel method that can help me download a specific model. What would be a good way to do this?
I installed "phpOffice\phpExcel" but I don't really know how to use it for my purpose.
You can find all the data you want to save to the excel, print that using an html table and setting a proper excel header before the view is rendered:
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
Source:
PHP Excel Header
I was searching already for a long time and I havent seen any right answer yet.
I'm trying to create a system in PHP where the user can download a signPicture that I create in JPG.
The program is working fine in all desktop computers. There is not problem at all, even for IE8.
The header that I use:
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.jpg"');
in the end i just stream the picture:
imagejpeg($imgSign,NULL,100);
How I said, it's working really good in every browser. But then we get to the mobile devices, where in android for example, download a test.jpg file... but then it cannot open... and the same with ipad (actually doesnt download, it show the image in the browser and than I save it... but it does not open either).
I also try more examples that I saw, but doesnt change anything, like:
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/download");
header("Content-Transfer-Encoding: binary ");
Any idea how to sort this out in mobile devices?
Thanks!
I got it!
There were differents problems. I found the clear solution in comments from this post:
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.JPG"');
The important steps: I send everything with a form. The form, to make it work in mobiles, needs to have the target='_top' and the method='get'
It also make errors if the extention (jpg) is not in UPPERCASE and the file name is not between " ".
Now it works in all devices that I try by far. :)
Special thanks to Jörg Wagner, author of the post.
I'm doing a script that increases the counter for an APK file's download then sends the file to the browser for download.
Here's what I have:
<?php
$file = "android.apk";
function force_download($file){
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
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=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));
}
force_download($file
The problem is that with a browser like firefox, it downloads but it is like 'android.apk - 0 bytes'. So it essentially, it does not download the file's contents.
What might I be doing wrong? A solution for this?
IMPORTANT: It has to work on mobile.
);
I've never accessed a .apk link that didn't force a download, so I'm not sure what the need for a force download is on that. As far as incrementing the counter, I would probably just link to a page that forwards to the apk file after the counter has been done.
For instance link someone to: getapk.php?apkid=1
Then on getapk.php do something like this:
$update = mysql_query("UPDATE apps SET downloads...");
if ( $update ) { header("Location: appname.apk"); }
Of course that leaves out a lot of details, but if you need help with anything else I'd be happy to provide more details.
I've realized that I don't need to use complex header info expecially if the script will be moved from server to server where the .apk mime type is not native and may therefore be hard for a novice to set up.
A simple redirect will do:
$file_name = $_GET['f']; //$_GET['f'] has the link to the file like http://mydomain.com/file/android.apk
//Do database query or increase download counter
header('location: '.$file_name);
Voila! I have increased the counter and the download will be pushed to the browser.
I have a link on my web page to download a .CSV file that I have generated on the server. The code for the download is as follows:
//open/save dialog box
header('Content-Disposition: attachment; filename="inventoryData.csv"');
//content type
header('Content-type: application/excel');
//read from server and write to buffer
readfile('spreadsheet/inventory.csv');
When I open the file on the server, it looks just fine. However, when I download the file via the dialog box, it is pre-pending the HTML code for the web page to the .csv file.
Any ideas why that would happen?
If this code is in a controller action which I assume it is since you are using ZF, then you need to disable your layout and the view renderer as it will try to render a view.
Try:
public function downloadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
//...
//open/save dialog box
header('Content-Disposition: attachment; filename="inventoryData.csv"');
//content type
header('Content-type: application/excel');
//read from server and write to buffer
readfile('spreadsheet/inventory.csv');
}
$this->_helper->layout()->disableLayout(); prevents your layout script from being rendered (assuming you use layouts), and $this->_helper->viewRenderer->setNoRender(true); tells the view renderer not to render the view script for the controller action which may contain some HTML or whitespace.
This should do the trick
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"inventoryData.csv\";" );
header("Content-Transfer-Encoding: binary");
Try this one:
header("Content-type: application/octet-stream");
header("Content-disposition:attachment; filename=inventoryData.csv");
I try to force a file-download via PHP with
$ctype="application/zip";
header("Content-Type: $ctype");
header("Content-Length: ".filesize($filepath));
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //header("Cache-Control: public");
header("Pragma: public");
header("Content-Disposition: attachment; filename=".$filename);
// header("Location: $filepath"); // edited: removed
readfile($filepath);
but it doesn't work.
with firebug I can see the changed header information but no save-file dialog appears...
You need to remove
header("Location: $filepath");
Which is basically redirecting you to the path specific instead of reading it's contents.
sendAndLoad() will "eat" your response and not cause a download which is useful if you're trying to load data. However, in your case you need to use getURL() since you want the browser to deal with the response, not flash.