如何强制pdf自动下载?

如何强制pdf自动下载?,pdf,download,Pdf,Download,是否有任何方法可以强制用户的下载管理器开始下载.PDF,而不是在新窗口/选项卡中显示.PDF?为此,您需要发送HTTP头(内容处置)。您不能在客户端执行此操作。在您的HttpResponse标头中设置内容配置: Content-Disposition = 'attachment; filename=filename.pdf' 这需要在服务器端完成。你不能在客户端这样做 如何做到这一点取决于所讨论的服务器端语言 PHP: 爪哇: .NET: Response.AddHeader("Content

是否有任何方法可以强制用户的下载管理器开始下载.PDF,而不是在新窗口/选项卡中显示.PDF?

为此,您需要发送HTTP头(
内容处置
)。您不能在客户端执行此操作。

在您的HttpResponse标头中设置内容配置:

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

这需要在服务器端完成。你不能在客户端这样做

如何做到这一点取决于所讨论的服务器端语言

PHP:

爪哇:

.NET:

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
如果没有任何服务器端代码来传输PDF文件,那么您需要在Web服务器级别对其进行配置。例如,在Apache HTTPD中,您可以使用以下条目在PDF文件夹的根目录中放置/扩展
.htaccess
文件:

<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>
<%@ 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();   
 %>

下载PDF.JSP代码:-

<a href='downloadPdf.jsp?file=FILE.pdf' >Download PDF File</a>
<%@ 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();   
 %>

= 0) {
位=in.read();
输出。写入(位);
}
} 
捕获(ioe异常ioe){
ioe.printStackTrace(系统输出);
}
out.flush();
out.close();
in.close();
%>
来源(这是我的博客):

对于IIS:

将所有要强制下载的文件放在它们自己的文件夹中

然后在IIS中转到该文件夹并双击HTTP响应头

添加包含以下信息的新标题:

姓名: 内容配置

价值: 附件:


访问该文件夹中的所有文件时,应提示相应浏览器的“另存为”对话框。

使用
中的下载属性
<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile('path/to/filename.pdf');

从互联网上的vb asp net代码中,我制作了这个简单的c#download.aspx页面。 您可以使用作为“f”querystring参数传递的文件url。(/folder/download.aspx?f=/folder1/example.pdf)


无效页面加载(对象发送方,事件参数e)
{
字符串strRequest=“”;
尝试
{
strRequest=Request.QueryString[“f”].ToString();
}
抓住
{ }
如果(strRequest.Length>0)
{
字符串路径=Server.MapPath(strRequest);
System.IO.FileInfo File=新的System.IO.FileInfo(路径);
如果(File.Exists)
{
Response.Clear();
Response.AddHeader(“内容处置”、“附件;文件名=“+File.Name”);
AddHeader(“Content-Length”,File.Length.ToString());
Response.ContentType=“应用程序/八位字节流”;
Response.WriteFile(File.FullName);
Response.End();
};
}
}

工作非常好,不必创建专门用于强制下载文件的页面。使用此功能并在Chrome、Firefox和IE8中进行了测试。一切都好!简单易用。注意:如果您使用
download=“filename”
甚至可以更改下载的名称!该死……我必须纠正我自己!它在IE上不起作用。我在虚拟机上测试了它,虚拟机上还没有安装pdf阅读器。在这种情况下,文件总是被下载。对于不想更改其服务器配置的人来说,这是一个很好的解决方案。该属性是在HTML5中添加的。相关:Hmm-有人知道如何做相反的事情吗?比如一个扩展来强制PDF在新选项卡中打开?我讨厌被迫下载;文件名=
部分。请进一步说明?
<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile('path/to/filename.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>