简体   繁体   English

使用具有自定义文件名的 ASP.NET 提供 PDF 文件

[英]Serve PDF file using ASP.NET with custom filename

I currently have a page ReportPDF.aspx which creates a PDF and writes it to the response.我目前有一个页面 ReportPDF.aspx,它创建一个 PDF 并将其写入响应。 I set the filename using the Content-Disposition header.我使用 Content-Disposition header 设置文件名。 However, if someone uses "Save link as..." on the link, it still saves the file as ReportPDF.aspx.但是,如果有人在链接上使用“将链接另存为...”,它仍会将文件另存为 ReportPDF.aspx。

How can I use Report.pdf as filename, while still dynamically creating the PDF?如何使用 Report.pdf 作为文件名,同时仍然动态创建 PDF?

I'm afraid that's a behaviour of the browser that the client is using - some will (correctly) use the content-disposition header and some won't... sorry.恐怕这是客户端正在使用的浏览器的一种行为 - 有些会(正确地)使用内容配置 header 而有些不会......对不起。

Could you use URL rewriting so that the URL is actually Report.pdf, which is then rewritten to ReportPDF.aspx server-side?能否使用 URL 重写,使 URL 实际上是 Report.pdf,然后重写到 ReportPDF.aspx 服务器端?

I had the same issue, and have it work with "clean" names:我遇到了同样的问题,并且可以使用“干净”的名称:

  • no spaces没有空间
  • only letters and numbers只有字母和数字

I solved it by linking to我通过链接解决了它

/ReportPDF.aspx/d6bea1cb-c630-47d6-8735-db7b72713fde/ReportData.pdf

And using the Request.PathInfo variable to get the parameter.并使用Request.PathInfo变量来获取参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM