Chrome PDF浏览器"保存到磁盘"功能-是否可以控制保存的文件扩展名

本文关键字:quot 保存 控制 是否 扩展名 文件 磁盘 浏览器 PDF Chrome 功能 | 更新日期: 2023-09-27 18:06:58

. NET 2.0应用程序,在Windows上使用Google Chrome 13。

当用户浏览到一个特定的aspx页面时,我的应用程序动态地生成一个PDF报告。在大多数情况下,在各种浏览器上都能正常工作。

然而,在Chrome上,当使用Chrome的PDF查看器时,可能会发生以下情况:-用户按查看器右下角的浮动磁盘图标保存PDF文档。该文件以aspx页面的名称保存。例如报告。aspx。

如果我在adobereader中打开下载的aspx文件,它可以作为PDF文档打开。但是有没有办法让chrome默认文件名保存为"。pdf"扩展名?

编辑:

报告生成页面的代码看起来像这样:
protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/pdf";
    byte[] data = GenerateReportHere(); // dynamically generate PDF report
    Response.AddHeader("Content-Length", data.Length.ToString());
    Response.BinaryWrite(data);
    Response.Flush();
    Response.End(); 
}

注意,我不想使用像这样的"Content-Disposition"头:

    Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");

,因为这会导致浏览器询问用户是否要下载该文件。在Chrome中,它不会在其PDF查看器中显示它-只是让用户在下载后使用他们与"。PDF"文件扩展名相关联的任何程序打开文件的机会。

Chrome PDF浏览器"保存到磁盘"功能-是否可以控制保存的文件扩展名

您应该尝试在流式传输PDF文件到浏览器时使用content-disposition标头。例如,

HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=[ReportName].pdf");
// Code to stream pdf file content to the client
...

有关内容配置的更多信息,请参见

  • http://www.jtricks.com/bits/content_disposition.html
  • http://greenbytes.de/tech/webdav/draft-reschke-rfc2183-in-http-latest.html

由于chrome是最新的HTML5,我们可以使用闪亮的新download属性!

<a href="http://www.domain.com/painful.pdf" download="newname">Works</a>