HTML image on webpage to download PDF File [duplicate] - c#

This question already has answers here:
(HTML) Download a PDF file instead of opening them in browser when clicked
(16 answers)
Closed 9 years ago.
Is there a sample way for the user to download, save the PDF file I have on my webpage?
Currently I have:
<tr><td><img src="../../images/speakernotes.png"/>
which just opens the file and does not save it. Looking for a easy way to do this. Thanks in advance.

If you are using HTML5 you can use the download attribute. Here is an example by Google.
Another example.

Yes, you can use the content-disposition header which will force the Save As.. dialog to the user.
So on your image, you can link to say a handler which will serve the PDF, or perhaps do a postback in an ImageButton and then serve the file.
String FileName = "FileName.pdf";
String FilePath = Server.MapPath("~/yourPath");
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();

Related

Save As Dialog box in asp.net

I have web application(asp.net).is there any way to show a 'SAVE AS DIALOG BOX'(in browser) when user clicks the download button.i have tried several codes but i didn't get expected result.all are download the file normally to downloads(default download path of browser).is it possible to open save as dialog box in web browser
Already answered:
[Save dialog box to download file, Saving file from ASP.NET server to the client
Probably you're looking for download a file in asp.net?
Please see sample below.
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
Response.TransmitFile(Server.MapPath("~/Files/MyFile.pdf"));
Response.End();
.htm, .html Response.ContentType = "text/HTML";
.txt Response.ContentType = "text/plain";
.doc, .rtf, .docx Response.ContentType = "Application/msword";
.xls, .xlsx Response.ContentType = "Application/x-msexcel";
.jpg, .jpeg Response.ContentType = "image/jpeg";
.gif Response.ContentType = "image/GIF";
.pdf Response.ContentType = "application/pdf";
How to download a file in ASP.Net

Viewing PDF in IE, hitting back navigates back 2 pages

Somewhat new to asp.net using c#. On my site, users have the option to view a pdf in the browser window. The pdf data is stored in an SQL database and I use the following code:
Byte[] bytes = (Byte[])dt.Rows[0]["ContentData"];
Response.ClearHeaders();
Response.Clear();
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", "inline; filename=" + dt.Rows[0] ["FileName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
The issue I am having only in Internet Explorer is when the user hits the back button, it navigates back 2 pages. It's not critical, but annoying to the users. I apologize if this has been asked, but I was having trouble coming up with search terms to find the issue.
Any help is appreciated. Thanks.
csnewbie

Download file from asp webapplication

I am working on an asp webapplication and i want the user to be able to download a file from a page. So when the user clicks on the "download template file" button it will start downloading a copy of the file to the computer. How can i do that?
To directly download file to your computer on button click just write this code on click event of button.
string filename = "~/File/yourFolder/"+ FileName;
string path = MapPath(filename);
byte[] bts = System.IO.File.ReadAllBytes(path);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "Application/octet-stream");
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
Else if you are having hyperlink just give the path of file in navigate url
The user has to download a template file.
As you mentioned in a comment that the user just needs to download the file. So, you must already have the url for the template file that is placed on the server. like
www.testwebsite.com/templatefile.xls
Add a simple link button in the website and in the href/Navigate URL add your template url. Whenever the user will click it, the file will be automatically downloaded.

c# asp.net click button to "save as", but downloaded file becomes unintentionally modified

I have a method in my C# codebehind that connects to SQL and creates a CSV file based upon the SQL data. This method is called when the user clicks a particular button on the website. I am trying to get this same method to also prompt the user to "open" or "save as" after the CSV file is generated.
I have tried the following:
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment; filename=" + "C:\\temp\\report.csv" + ";");
Response.TransmitFile("C:\\temp\\report.csv");
The user does get prompted to "open or save as". In either case, the data in the CSV file also contains the entire source code of my Default.aspx. What do I need to do to my code to prevent that extra data from being appended onto my CSV? TIA...
You can call response.End() to prevent further processing by your page.
You need to clear the Response first - Response.Clear();
response.Clear();
...
response.AddHeader("Content-Disposition", "attachment; filename=" + "C:\\temp\\report.csv" + ";");
Response.TransmitFile("C:\\temp\\report.csv");

display *doc,*xls,*pdf in browser window instead

i want to display *doc,*xls,*pdf in browser window instead of their respected application. i have tried following code, but no luck it prompt me dialog for save/open instead of displaying in browser
//Set the appropriate ContentType.
Response.ContentType = "Application/msword";
//Get the physical path to the file.
string FilePath = MapPath("wordfile.doc");
//Write the file directly to the HTTP content output stream.
Response.AppendHeader("Content-Disposition", "inline;filename=" + "wordfile.doc");
Response.WriteFile(FilePath);
Response.End();
please help
thanks
for any of these proprietary formats you need browser plugins/extensions or ActiveX for IE
Am not sure if its available for office formats.
For PDF setting the content type to "application/pdf" and Content-Disposition to inline should work.

Categories