Setting PDF file name dynamically

2019-05-21 06:10发布

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:

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. 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.

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?

Thanks in advance. badallen

2条回答
放我归山
2楼-- · 2019-05-21 07:03

This is a simple behavior of the way that acrobat handles the file when you have it open inline. As far as I know there is not a way to dictate this when opening inline, as that becomes the responsibility of Acrobat.

查看更多
混吃等死
3楼-- · 2019-05-21 07:09

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).

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)

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; the article shows how you can use a dynamically named HttpHandler to deliver PDFs and control the filename.

Fair warning: I haven't tried this yet myself so I can't tell you if it will work.

查看更多
登录 后发表回答