How to force a pdf download automatically?

2019-01-02 23:40发布

Is there any way to force the user's download-manager to start a download for .PDF instead of showing the .PDF in a new window/tab?

标签: pdf download
10条回答
贪生不怕死
2楼-- · 2019-01-03 00:14

Set Content-Disposition in your HttpResponse header:

Content-Disposition = 'attachment; filename=filename.pdf'
查看更多
疯言疯语
3楼-- · 2019-01-03 00:15

For IIS:

Put all files you want to force to download in their own folder.

Then in IIS go that folder and double click HTTP Response Headers.

Add a new header with the following info:

Name: content-disposition

Value: attachment

All files in that folder, when accessed, should prompt the save as dialog box for the appropriate browser.

查看更多
相关推荐>>
4楼-- · 2019-01-03 00:19
    <?php
    // required for IE, otherwise Content-disposition is ignored   
     if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
    }
$reader = fopen($filename, "r");
$contents = fread($reader, filesize($filename));
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    ob_end_clean();
    echo $contents;
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-03 00:19

From vb asp net code found on the internet i made this simple c# download.aspx page. You can use with the file url passed as "f" querystring parameter. (/folder/download.aspx?f=/folder1/example.pdf).

<!DOCTYPE html>
<html>
<head><title></title>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {

        String strRequest = "";
        try
        {
            strRequest = Request.QueryString["f"].ToString();
        }
        catch
        { }

        if (strRequest.Length > 0)
        {
            String path = Server.MapPath(strRequest);
            System.IO.FileInfo File = new System.IO.FileInfo(path);
            if (File.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + File.Name);
                Response.AddHeader("Content-Length", File.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(File.FullName);
                Response.End();
            };
        }
    }
</script>
</head>
<body></body>
</html>
查看更多
一纸荒年 Trace。
6楼-- · 2019-01-03 00:22
<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile('path/to/filename.pdf');
查看更多
混吃等死
7楼-- · 2019-01-03 00:23

This needs to be done in the server side. You can't do this at the client side.

How to do it depends on the server side language in question.

PHP:

header('Content-Disposition: attachment; filename="' . $filename . '"');

Java:

response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

.NET:

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

If there's no means of any server side code which streams the PDF file, then you need to configure it at webserver level. In for example Apache HTTPD, you can place/expand a .htaccess file in the PDF folder's root with the following entry:

<Files *.pdf>
    Header set Content-Disposition attachment
</Files>

or configure it globally in httpd.conf file. Similar approach exist for IIS with web.config file.

查看更多
登录 后发表回答