简体   繁体   English

动态设置PDF文件名

[英]Setting PDF file name dynamically

I hope I am able to explain my situation accurately. 我希望我能够准确地解释我的情况。

I have an ASP.NET page that dumps out PDF files for the users with the following code: 我有一个ASP.NET页面,它使用以下代码为用户转储PDF文件:

Response.ContentType = "application/pdf";                
Response.AppendHeader("content-disposition", string.Format("inline; filename={0}", getFileName(DateTime.Now)));

The reason why I use "inline" instead of "attachment", so I can force the users to view the PDF directly in the browsers instead of opening up the acrobat to view the PDFs. 之所以使用“内联”而不是“附件”,是因为我可以强迫用户直接在浏览器中查看PDF,而不是打开acrobat来查看PDF。 Everything is fine so far, but when users want to save a file, the file name which should be set to "getFileName(DateTime.Now)" , instead it just takes the name of the page like myPDFpage.pdf . 到目前为止,一切都很好,但是当用户要保存文件时,应将文件名设置为“ getFileName(DateTime.Now)” ,而只使用诸如myPDFpage.pdf之类的页面名称。

Is there anyway, without setting the content-disposition to "attachment", for the users to save the file with the name I specify with the getFileName() method? 无论如何,在没有将content-disposition设置为“ attachment”的情况下,用户可以使用我用getFileName()方法指定的名称来保存文件吗?

Thanks in advance. 提前致谢。 badallen badallen

This is a simple behavior of the way that acrobat handles the file when you have it open inline. 这是acrobat内联打开文件时处理文件的一种简单行为。 As far as I know there is not a way to dictate this when opening inline, as that becomes the responsibility of Acrobat. 据我所知,当打开内联时,没有任何方法可以规定这一点,因为这成为Acrobat的责任。

I understand what you're trying to do is not possible because there are problems wth the 'inline' disposition type (I gather it's a PDF and/or browser issue). 我了解您无法执行的操作是因为“内联”处置类型存在问题(我认为这是PDF和/或浏览器问题)。

I was struggling with the same issue and found this article which promises a solution: 我在同一个问题上苦苦挣扎,发现这篇文章提出了解决方案:
How to show or download a pdf file from an ASP.NET 2.0 page (iTextSharp version) 如何从ASP.NET 2.0页面(iTextSharp版本)显示或下载pdf文件

The basic problem is that by default PDFs will be saved with a base name (name without filename extension) of the page it's served up on; 基本的问题是,默认情况下,PDF将使用其所服务页面的基本名称(不带文件扩展名的名称)保存; the article shows how you can use a dynamically named HttpHandler to deliver PDFs and control the filename. 本文介绍了如何使用动态命名的HttpHandler来传递PDF和控制文件名。

Fair warning: I haven't tried this yet myself so I can't tell you if it will work. 合理的警告:我自己尚未尝试过此操作,因此无法告诉您是否可行。

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

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