i have tried and downloaded using ".xlsx". It will be downloaded in new tab and asks to user where to save the particular file.
But when i have tried same for xml download, it does not happen.
Here is the code for .xlsx
fileName = downloadFileName + "_" + currentTime + ".xlsx";
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
System.Web.HttpContext.Current.Response.BinaryWrite(xlPackage.GetAsByteArray());
System.Web.HttpContext.Current.Response.End();
System.Web.HttpContext.Current.Response.SuppressContent = true;
This code works fine when downloading for xlsx. Below code is used to download for Xml, Which i could able to do.
string downloadFileName = "Template_Xml";
string currentTime = DateTime.Now.ToString("ddMMyyyyhhmmss");
fileName = downloadFileName + "_" + currentTime + ".xml";
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Web.HttpContext.Current.Response.ContentType = "text/xml";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
XmlDocument docConfig = new XmlDocument();
XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
XmlElement rootElement = docConfig.CreateElement("User");
docConfig.AppendChild(rootElement);
XmlElement environmentElement = docConfig.CreateElement("NAME");
XmlText environText = docConfig.CreateTextNode("ABC" + i);
environmentElement.AppendChild(environText);
hedder.PrependChild(environmentElement);
System.Web.HttpContext.Current.Response.BinaryWrite(docConfig);
System.Web.HttpContext.Current.Response.End();
What I have tried:
Here is the code for downloading XML,
<pre> string downloadFileName = "Template_Xml";
string currentTime = DateTime.Now.ToString("ddMMyyyyhhmmss");
fileName = downloadFileName + "_" + currentTime + ".xml";
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Web.HttpContext.Current.Response.ContentType = "text/xml";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
XmlDocument docConfig = new XmlDocument();
XmlNode xmlNode = docConfig.CreateNode(XmlNodeType.XmlDeclaration, "", "");
XmlElement rootElement = docConfig.CreateElement("User");
docConfig.AppendChild(rootElement);
XmlElement environmentElement = docConfig.CreateElement("NAME");
XmlText environText = docConfig.CreateTextNode("ABC" + i);
environmentElement.AppendChild(environText);
hedder.PrependChild(environmentElement);
System.Web.HttpContext.Current.Response.BinaryWrite(docConfig);
System.Web.HttpContext.Current.Response.End();