force browser to download a file instead viewing it

前端 未结 2 710
野的像风
野的像风 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: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");
    

提交回复
热议问题