2010-04-08 86 views
30

有什麼辦法可以強制用戶的下載管理器爲.PDF啓動下載,而不是在新的窗口/標籤中顯示.PDF?如何自動強制PDF下載?

+0

相關:http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php – 2014-11-07 12:02:12

回答

3

您需要發送HTTP標頭(Content-disposition)才能執行此操作。你不能在客戶端做到這一點。

25

集內容處置你的HttpResponse標題:

Content-Disposition = 'attachment; filename=filename.pdf' 
+0

的'; filename = '部分[是可選的](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax)。 – 2017-01-17 22:12:55

0
<?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; 
12

這需要在服務器端完成。你不能在客戶端做到這一點。

如何做到這一點取決於有問題的服務器端語言。

PHP:

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

的Java:

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

.NET:

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

如果有哪個流的PDF文件中的任何服務器端代碼沒有辦法,那麼你就需要在網絡服務器級別進行配置。在例如Apache的HTTPD,你可以把/擴展.htaccess文件中的PDF文件夾的根目錄中的以下項:

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

httpd.conf文件全局配置它。對於具有web.config文件的IIS,存在類似的方法。

1

的.htaccess解決方案:

AddType application/octet-stream .pdf 
2

是它可以在JSP頁面來完成......通過給出一個JSP頁面下載鏈接都到新的腳本網頁...並下載PDF文件如下

DownloadPage.JSP代碼: -

<a href='downloadPdf.jsp?file=FILE.pdf' >Download PDF File</a> 

downloadPdf.JSP代碼: -

<%@ page import="java.util.*,java.io.*"%>    

<% 

    File f = new File ("E:/PDFfiles/Downloads/" + request.getParameter("file")); 
    response.setContentType ("application/pdf"); 
    response.setHeader ("Content-Disposition", "attachment; filename=""+request.getParameter("file")+"""); 
    String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length()); 
    InputStream in = new FileInputStream(f); 
    ServletOutputStream outs = response.getOutputStream(); 


    int bit = 256; 
    int i = 0; 
    try { 
    while ((bit) >= 0) { 
    bit = in.read(); 
    outs.write(bit); 
        } 
     } 
     catch (IOException ioe) { 
       ioe.printStackTrace(System.out); 
      } 
    outs.flush(); 
    outs.close(); 
    in.close(); 
%> 

來源:http://bloggerplugnplay.blogspot.in/2012/05/how-to-create-force-download-link-for.html

+0

太棒了!謝謝!請注意,文件名可能需要雙反斜槓\\。此外,在我看來,在這段代碼中並沒有使用字符串'name' – gordon613 2014-11-20 12:39:00

7

對於IIS:

將要強制在自己的文件夾中下載的所有文件。

然後在IIS中去那個文件夾並雙擊HTTP Response Headers。

添加一個新的頭具有以下信息:

名稱: 內容處置

值: 附件

該文件夾中的所有文件,訪問時,應提示另存爲對話框適當的瀏覽器框。

+1

工作得很好,不需要專門爲強制文件下載創建一個頁面。 – Edyn 2013-11-03 03:54:48

3
<?php 
header('Content-disposition: attachment; filename=filename.pdf'); 
header('Content-type: application/pdf'); 
readfile('path/to/filename.pdf'); 
30

使用下載屬性您<a>標籤

<a href="content/file.pdf" download > pdf link </a>
<a href="content/file.doc" download > doc link </a>

+1

這實際上只適用於Chrome。 – 2013-12-10 23:43:18

+1

使用它並在Chrome,Firefox和IE8中測試它。這一切正常!簡單而簡單。注意:如果你使用'download =「filename」'你甚至可以改變下載的名字! – Marja 2014-10-29 13:04:44

+0

該死......我必須糾正自己!它不適用於IE。我在虛擬機上進行了測試,其中尚未安裝pdf閱讀器。在那種情況下,文件總是被下載。 – Marja 2014-10-31 11:32:23

0

從VB ASP .NET代碼我做了這個簡單的C#download.aspx網頁在互聯網上找到裏面。 您可以使用以「f」querystring參數傳遞的文件url。 (/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>