Reputation: 195
SOLVED: View the answer below marked SOLVED. Thanks.
I am trying to download a file from location specified by url on click. I am using a delegate but when the button clicks the browser navigates to the location of the file and displays the contents. I want the click to trigger the browser's download dialog.
Here is the code:
$('#left').delegate("a", "click", function(event){
//alert("CLICKED");
event.preventDefault();
window.location.href = url;
});
All posts regarding this question says to use the "event.preventDefault();" but that does not work for me. Does anyone have any suggestions?
Upvotes: 1
Views: 4540
Reputation: 195
SOLVED: The following php script allowed prompting the browser dialog box with the file:
header('Content-Disposition: attachment; filename="' . $filename . '"');
$fp=fopen($filepath,'r');
fpassthru($fp);
fclose($fp);
Thank you deantoni for leading me in the correct direction =).
Upvotes: 1
Reputation: 1968
You may need to change the server side (if you are able to).
The http response must contain the header:
Content-disposition: attachment; filename=fname.ext
So the browser knows that it must open the download dialog. Otherwise the browser will try to open/preview it.
More info:
Upvotes: 4