clime
clime

Reputation: 8885

force browser to download a file instead viewing it

On my web I want to have two buttons - first Download that should download a pdf to the user's computer and another View that opens pdf in a new tab. The thing is, I don't know how to make the Download button. You might say that it is useless as you can save pdf after "viewing" it but I have already made graphics like that and don't really want to change it. Is it possible to prevent browser from opening the pdf file and make it download the file instead?

Upvotes: 2

Views: 1773

Answers (2)

clime
clime

Reputation: 8885

In the end, as I use nginx, I decided to solve this at http-server level:

location /media  {
    alias /srv/www/novacek/media/;
}

location /download/media {
    alias /srv/www/novacek/media/;
    add_header Content-Disposition "attachment";
}

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

You need to force file download in your server-side script:

Here is a PHP example:

$file = "path/to/my-file.pdf";

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");

Upvotes: 3

Related Questions