Download file on ImageButton Click event - c#

I have a link of a file, and I don't know how to add it to imagebutton. I want to see the 'open-save-cancel' , screen when imagebutton is clicked, but imagebutton doesn't seems to be taking the link of file as a file, but taking it as a page url and tring to open the file url as page.
Thanks

//OnClick server Handler
Response.Clear();
Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", "file_name"));
Response.ContentType = "application/octet-stream";
Response.WriteFile("file_path");
Response.End();

Related

Download file from database asp.net

I am reading PDF data from a database field (database field format is 'Data') and I have converted it into bytes using data reader GetBytes and I have it right because it's working fine in other operation.
I added a button on aspx page, and, on button click, I have the below code to download it:
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename="test.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(byteArray);
Response.End();
But it's not doing any thing. I mean when I click the button I can step through it but after response.end() nothing happens.
Any idea what I am doing wrong?
Move your button outside of update panel. You also need to add postback trigger as Oguz Ozgul mentioned it. Additionally you should add ThreadAbortException catch also just to stay safe...

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

XML file written to Response contains page HTML

I'm trying to write an XML file to the response when a button is clicked so the user can download the file. This works fine with an Excel file, but when I use the "text/xml" content type the file contains the expected contents, but with the webpage HTML appended to the end.
I assume since the button click is returning the page HTML it is merged with the file. I tried using Response.ClearContent() to try and clear the response, but it didn't work.
protected void Button1_Click(object sender, EventArgs e) {
string fileName = "myFile.xml";
string filePath = Server.MapPath("~/temp/myFile.xml");
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.ClearContent(); //I assumed this would clear the HTML before the file is written.
Response.WriteFile(filePath);
Response.Flush();
File.Delete(filePath);
Context.ApplicationInstance.CompleteRequest();
}
How do I make sure the page is not written to the XML file?
The HTML is being rendered after the button's Click event is raised, so clearing the content at that point won't have any effect.
Try adding Response.End(); to the end of your method.

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.

no more postback after file download in asp.net

After downloading a file from a webpage, I can't do anything on that page. It is like the page is not working anymore. When I click on other button, there is no post back.
Here are my codings.
filename= "test.txt"
Response.AppendHeader("content-disposition", "attachment; filename= " + fileName);
Response.WriteFile(Server.MapPath("~/" + fileName));
Response.End();
So I try to comment Response.End() but it is still not working.
What can be the possible problems?
UPDATE
Go to this link to see how I solved it.

Categories