XML file written to Response contains page HTML - c#

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.

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

HTML image on webpage to download PDF File [duplicate]

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

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