no more postback after file download in asp.net - c#

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.

Related

Writing data to Response, but keeping the page active

I'm trying to have a user click a button on a web page to download a CSV file, but I'm having problems sending the correct data to the user. In addition, once the file has been downloaded, I'd like the page to remain "active" i.e. the page can continue to trigger events to the server, such as clicking the Download button again.
This is the code I'm currently using, pieced together from various SO questions:
var sb = new StringBuilder();
string fileName = "data.csv";
// Build CSV file...
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("filename={0}", fileName));
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
This works so far as presenting the user with an option to open or download the file, but the file also contains the entire markup of the aspx file after the requested data, and the page is left completely inactive.
I'm guessing the problem is with the last two lines of the above code. I've tried using ApplicationInstance.CompleteRequest instead of Response.End, but this doesn't seem to change the outcome. The page is still inactive and the file still has the markup at the end. Can anyone help?
In case it makes any difference in the behaviour of Response, the page is an WebPartPage in SharePoint 2010.
Set the line
HttpContext.Current.Response.AddHeader("content-disposition", "filename={0}", fileName));
to
HttpContext.Current.Response.AppendHeader("content-disposition", String.Format("attachment;filename={0}", fileName));
And set the following property on your button
downloadButton.OnClientClick = "_spFormOnSubmitCalled = false;";
_spFormOnSubmitCalled is a javascript variable that SharePoint uses to stop multiple form submits and it is being set to true when you click the download button.
try this
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
HttpContext.Current.Response.ContentType = "application/CSV";

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");

Download file on ImageButton Click event

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();

Categories