0

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?

Trevor Tracy
  • 356
  • 1
  • 10
Tug Strongly
  • 187
  • 2
  • 5
  • 18
  • 3
    Xml is just text so you can save using StreamWriter(). – jdweng Jun 19 '18 at 14:08
  • I actually didn't want to create the file on the web server. I wanted the user to be able to just download the file from the .aspx page. The xml has sensitive data that I can't leave lying around in a folder. – Tug Strongly Jun 19 '18 at 14:42
  • Are you designing server code or a webpage? A webpage is run on the User PC and doesn't need to be downloaded. Just needs to be saved. – jdweng Jun 19 '18 at 15:01
  • It is server code. I want the xml to be downloaded and not saved. – Tug Strongly Jun 19 '18 at 16:19

2 Answers2

0

It looks like you've forgotten to rewind the memory stream before you write it's contents to the response

using (var stream = new MemoryStream(fileInBytes))
{
    stream.Seek(0, SeekOrigin.Begin);
    long dataLengthToRead = stream.Length;
    ...

But, as jdweng states, XML is just text and can be written directly to the response without the need for a MemoryStream.

phuzi
  • 12,078
  • 3
  • 26
  • 50
0

Here is a way that was posted on the asp.net forums. It uses the XMLDocument object instead.

XmlDocument Doc = new XmlDocument();
    XmlDeclaration dec = Doc.CreateXmlDeclaration("1.0", null, null);
    Doc.AppendChild(dec);
    XmlElement DocRoot = Doc.CreateElement("settings");
    Doc.AppendChild(DocRoot);

    XmlNode server = Doc.CreateElement("textbox1");
    DocRoot.AppendChild(server);
    server.InnerText = this.TextBox1.Text;

    XmlNode server2 = Doc.CreateElement("textbox2");
    DocRoot.AppendChild(server2);
    server2.InnerText = this.TextBox2.Text;

    Doc.Save(Application.StartupPath + "\\xmlfile.xml");
L0uis
  • 703
  • 5
  • 8
  • I actually didn't want to create the file on the web server. I wanted the user to be able to just download the file from the .aspx page. The xml has sensitive data that I can't leave lying around in a folder. – Tug Strongly Jun 19 '18 at 14:43
  • I see. Here's a way (at the bottom) to write the Doc to a response then: https://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net good luck! – L0uis Jun 19 '18 at 14:46