有什麼辦法可以強制用戶的下載管理器爲.PDF啓動下載,而不是在新的窗口/標籤中顯示.PDF?如何自動強制PDF下載?
回答
您需要發送HTTP標頭(Content-disposition
)才能執行此操作。你不能在客戶端做到這一點。
集內容處置你的HttpResponse標題:
Content-Disposition = 'attachment; filename=filename.pdf'
的'; filename =
<?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;
這需要在服務器端完成。你不能在客戶端做到這一點。
如何做到這一點取決於有問題的服務器端語言。
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,存在類似的方法。
的.htaccess解決方案:
AddType application/octet-stream .pdf
是它可以在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
太棒了!謝謝!請注意,文件名可能需要雙反斜槓\\。此外,在我看來,在這段代碼中並沒有使用字符串'name' – gordon613 2014-11-20 12:39:00
對於IIS:
將要強制在自己的文件夾中下載的所有文件。
然後在IIS中去那個文件夾並雙擊HTTP Response Headers。
添加一個新的頭具有以下信息:
名稱: 內容處置
值: 附件
該文件夾中的所有文件,訪問時,應提示另存爲對話框適當的瀏覽器框。
工作得很好,不需要專門爲強制文件下載創建一個頁面。 – Edyn 2013-11-03 03:54:48
<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile('path/to/filename.pdf');
使用下載屬性您<a>
標籤
<a href="content/file.pdf" download > pdf link </a>
<a href="content/file.doc" download > doc link </a>
從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>
- 1. 如何在IE上強制下載pdf?
- 2. 如何強制PDF下載初學者
- 3. 強制R&OS動態PDF下載php
- 4. 如何強制用戶下載圖像(如下載pdf)?
- 5. 使用PHP強制下載PDF
- 6. 從wordpress中強制下載PDF文件
- 7. 使用.htaccess強制PDF下載
- 8. PHP:強制PDF文件下載
- 9. 強制pdf下載點擊按鈕時
- 10. ASP.NET MVC強制PDF下載,不顯示
- 11. .htaccess在Safari中強制PDF下載
- 12. 使用htaccess強制PDF文件下載
- 13. 如何強制下載
- 14. 如何強制下載PHP?
- 15. Symfony2:如何強制下載
- 16. 防止PDF自動下載
- 17. RApache如何強制通過使用R的pdf下載
- 18. 如何強制下載/打開對話框顯示來自Javascript的PDF?
- 19. 強制下載
- 20. 如何強制鏈接到PDF以告訴瀏覽器將其下載爲PDF?
- 21. PHP強制下載
- 22. 強制下載ASP.Net
- 23. 如何強制加載自動加載類?
- 24. 如何在python中強制下載xml?
- 25. 如何強制Composer下載本地包?
- 26. 如何強制grails下載csv文件?
- 27. 如何強制Intellij IDEA下載JavaDoc
- 28. 如何強制下載Windows模塊
- 29. 如何在MVC2中強制下載?
- 30. 禁止自動下載pdf文件
相關:http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php – 2014-11-07 12:02:12