I want to make the browser download a PDF document from server instead of opening the file in browser itself. I am using C#.
Below is my sample code which I used. It not working..
string filename = "Sample server url"; response.redirect(filename);
Click “Site Settings” on the right. Scroll down in Site Settings and click “Additional content settings” at the very bottom. In the expanded menu, select “PDF documents.” Toggle on the “Download PDF files instead of automatically opening them in Chrome” option.
In Google Chrome go to Settings. Scroll to the bottom of the page and click on the hyperlink for Show Advanced Settings. From there scroll down until you find the "Download" section. Uncheck the box where it asks where to save each file before downloading.
You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=foo.pdf" will prompt the user (typically) with a "Save as: foo.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile
for this purpose. For example (assuming you aren't using MVC, which has other preferred options):
Response.Clear(); Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf"); Response.TransmitFile(filePath); Response.End();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With