ßee
ßee

Reputation: 195

Download file JQuery with delegated click button


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

Answers (2)

ßee
ßee

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

vdeantoni
vdeantoni

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:

Microsoft Support

Upvotes: 4

Related Questions