I have an XML file written in C# using StreamWriter with this code:
String filename = Session.SessionID + ".xml";
String filepath = "h:\\root\\home\\mchinni-001\\www\\site1\\OUTFolder\\" + filename;
StreamWriter sw = new StreamWriter(filepath, false);
I fill strOut with the formatted contents of my XML file then I do:
sw.WriteLine(strOut);
sw.Close();
When I check the file on the web server, it looks perfect (for my purposes).
I then try to download it with:
Response.ContentType = "application/xml";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
Response.TransmitFile(filepath);
Response.End();
That download has worked sometimes, but most of the time, I get a "missing file" notice in my downloads area & no file at all gets downloaded (not even a 0-length file).
How do I get this file downloaded?
I've read a bit about the "proper" way to write an XML file in C# but my code already generates a (for my purposes) properly formatted XML file & I'd rather not have to do a major/massive rewrite if I can avoid it.
BTW, I've also tried:
Response.ContentType = "text/xml";
and
Response.ContentType = "text/plain";
but neither of them ever worked.
Make sure to
flushtheStreamWriterbefore closing it. This ensures that all the data is written to the file before attempting to transmit it.Include
Response.BufferOutput = true;, it allows the server to buffer the output before sending it to the client, which can help in the context of file downloads.