force browser to download a file instead viewing it

前端 未结 2 681
野的像风
野的像风 2021-01-15 03:48

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 t

相关标签:
2条回答
  • 2021-01-15 04:02

    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";
    }
    
    0 讨论(0)
  • 2021-01-15 04:17

    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");
    
    0 讨论(0)
提交回复
热议问题