i want to create a downloadscript which allows Force Download of JPGs. This is my php script:
<?php
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: image/jpg");
header('Content-Disposition: attachment; filename="'.basename($GET['a']).'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize(($GET['a']));
readfile(($GET['a']);
?>
This is a code segment of my js code:
function downloadFile(a){
document.location = "download.php?a="+ a;
}
With this code sample nothing happens. If i append the result into a HTML-tag, it shows the content of the file.
Any ideas how to teach the browser to download this file?
EDIT: SCRIPT UPDATE
Setting the mime type to
image/jpeg
will most probably not work. So, you needapplication/octet-stream
instead to force the download.Replace the content type header in your php with the following:
Also, One nice solution instead of using
document.location
is to inject an iframe. Use the following function in your success callbackYou're getting it confused a bit. As FAngel pointed out, you can't download files via AJAX. What you need to do is redirect the user to another page that then has your above PHP code in it. That PHP code should then allow the user to download the file directly. What you're attempting is absolutely possible, you just need to approach it from another direction, ie not with AJAX.
You can't download files with ajax. So, if you have something that should happen on ajax, you should return url in response and apply it like
document.location = "url"
to start download process.One note here. As I remember, browser will block file download if it is initiated not by user click. So, this will work fine:
But if it is started not by user click, it will be blocked. So, code like this:
will be blocked by browser. So, if you want to pass some data with a post, you may submit a form into hidden iframe or to blank page using
<form target="..."
:And in script.php you need to execute code from download.php immediately, if token is Ok, or do a redirect to download script:
You can force download file with Mouse middle event:
It seems you have errors in your script. First of all, correct speliing for GET variable is $_GET['a'], not $GET['a']. The second issue here is that you have extra opening parenthesis, when I copied your code, I received 500 Internal Server Error response. If we correct mistakes, it seems to work fine. Just try corrected version of your code.