Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm downloading a PDF to be viewed on the browser within the content section of a master page form. I would like my user to have the ability to click a download button or right click and SAVE AS the name of the PDF.

I think my problem is that the pdf files are on a network share and cannot be accessed directly by the user. I found that if the files were directly accessible ( for example mywebsite.com/PdfFile.pdf), the name downloaded to the user is the same as the pdf file and all the download capability is there. However, all my files are on network shares and I must use an .aspx page to serve up the pdf file ( for example mywebsite.com/PdfServer.aspx?file=PdfFile.pdf). When I do this, the displayed page is named after the .aspx file and I cannot directly download the file as a pdf.


Any help to be pointed in the right direction appreciated.

Larry

What I have tried:

I did figure out that I could save the .aspx page as a PDF but that's not a good solution.

I did locate some behind code that displayed the PDF in a standalone page:
Response.Redirect("~PdfFile.pdf");

That meets all my requirements like the right name being displayed and the download capability being present. However, this appears to only work when the PDF is within the website folders. My files are on network drives so I have to use aspx or ashx pages to transmit them.

I've also tried to use something like the following and it worked great, the document displayed within a PDF frame with download options...
<asp:HyperLink ID="lnkPDF" runat="server" NavigateUrl="~PdfFile.pdf" Target="_blank">View PdfFile.pdf</asp:HyperLink>

but when I changed the NavigateURL to my aspx page serving up the PDF the file would display but with the aspx extension and none of the download or print features available.

I've played around with the TransmitFile, WriteFile, contentTypes and Content-Disposition (attachment & inline) headers with no success.
Posted
Updated 15-Mar-17 8:14am

1 solution

This is how you give it a filename.
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Most browsers will use that to default the name that is displayed when saving.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900