[英]How to force a pdf download automatically?
有沒有辦法強制用戶的下載管理器開始下載.PDF,而不是在新窗口/選項卡中顯示.PDF?
使用<a>
標記內的download屬性
<a href="content/file.pdf" download > pdf link </a>
<a href="content/file.doc" download > doc link </a>
在HttpResponse標頭中設置Content-Disposition:
Content-Disposition = 'attachment; filename=filename.pdf'
這需要在服務器端完成。 你不能在客戶端這樣做。
怎么做取決於有問題的服務器端語言。
PHP:
header('Content-Disposition: attachment; filename="' . $filename . '"');
Java的:
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
。凈:
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
如果沒有任何服務器端代碼可以流式傳輸PDF文件,那么您需要在Web服務器級別進行配置。 在例如Apache HTTPD中,您可以使用以下條目將.htaccess
文件放在/擴展到PDF文件夾的根目錄中:
<Files *.pdf>
Header set Content-Disposition attachment
</Files>
或者在httpd.conf
文件中全局配置它。 對於帶有web.config
文件的IIS,存在類似的方法。
對於IIS:
將要強制下載的所有文件放在自己的文件夾中。
然后在IIS中轉到該文件夾並雙擊HTTP Response Headers。
使用以下信息添加新標頭:
名稱:內容配置
價值:附件
訪問該文件夾中的所有文件時,應提示相應瀏覽器的“另存為”對話框。
您需要發送HTTP標頭( Content-disposition
)才能執行此操作。 你不能在客戶端這樣做。
<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile('path/to/filename.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
.htaccess解決方案:
AddType application/octet-stream .pdf
<?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;
從互聯網上找到的vb asp網絡代碼我做了這個簡單的c#download.aspx頁面。 您可以使用文件url作為“f”查詢字符串參數傳遞。 (/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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.