I have a asp text box that displays XML information. It looks like an XML file. I need to be able to allow a user to download a file that is created from the text box contents. I am using the following C# code.
protected void btnDownload_Click(object sender, EventArgs e)
{
var fileInBytes = Encoding.UTF8.GetBytes(tXML.Text);
using (var stream = new MemoryStream(fileInBytes))
{
long dataLengthToRead = stream.Length;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "text/xml"; /// if it is text or xml
Response.AddHeader("Content-Disposition", "attachment; filename=" + "yourfilename.xml");
Response.AddHeader("Content-Length", dataLengthToRead.ToString());
stream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
}
Response.End();
}
When I try to download it with Chrome, I get Failed - Network Error. When I try to download it with IE, it'll download, but when I view the contents all the "<" and ">" are stripped from it. I know it could be a security issue downloading some file types, but an XML file? Is there a better way to do this?