Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an XML file with ASP.NET and have it prompt to download?

Tags:

asp.net

I'm currently trying to write data (client machine) into a xml file where the user can save. However, I want the users to be able to decide on where they want to save this written xml file. Is there any controls or codes that I can use to allow the users to save the file?.

Update:

is that right way to do?

**HttpContext.Current.Response.Write(xw.ToStroing()); <<< ??????**
HttpContext.Current.Response.End(); 

update:

XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            MemoryStream ms = new MemoryStream();

HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "text/xml";
            HttpContext.Current.Response.AddHeader("Content-Disposition:", "attachment;filename=" + HttpUtility.UrlEncode(fileName));        

            using (StringWriter sw = new StringWriter())
            {
                using (XmlWriter xw = XmlWriter.Create(ms, settings))

                {
                    xw.WriteStartDocument();

                    xw.WriteStartElement("Name");
                    xw.WriteStartElement("Application");
                     ................
                    ......................
                    HttpContext.Current.Response.Write(xw.ToStroing());
                      HttpContext.Current.Response.End(); 
like image 613
Nick Kahn Avatar asked Nov 06 '22 11:11

Nick Kahn


1 Answers

When you begin a file download, the client's browser will handle the file saving location.

If you want to force the file to download instead of open, add this HTTP header to your response:

Content-Disposition: attachment; filename="myFile.xml"
like image 130
Dave Thieben Avatar answered Nov 15 '22 05:11

Dave Thieben