ASP.NET 将XML文件下载到本地

 ///
    /// 生成导出的XML文件 并实现下载
    ///

    /// 生成的文件名称
    private void SetXMLFile(string condition)
    {
        DataTable dt = null;
        if (dwzdb != null)
        {
            //根据日期查询要导出的数据
           
            switch (flag)
            { 
                case "LZDB":
                    dt = Family.GetCheckList(stepId, dwzdb.Code, flag, condition);
                    break;
                case "LZDBNS":
                    dt = BussinessUtilNS.GetCheckList(nsnf, dwzdb.Code,flag, condition);
                    break;
            }
        }
        if (dt != null)
        {
            //生成要导出的xml文档
            string xml = "";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlElement element = doc.DocumentElement;
            XmlElement houseCheckNode = null;
            XmlElement childNode = null;
            foreach (DataRow dr in dt.Rows)
            {
                houseCheckNode = doc.CreateElement("HOUSECHECK");
                childNode = doc.CreateElement("JTBH");
                childNode.InnerText = dr["JTBH"].ToString();
                houseCheckNode.AppendChild(childNode);
                childNode = doc.CreateElement("XM");
                childNode.InnerText = dr["XM"].ToString();
                houseCheckNode.AppendChild(childNode);
                childNode = doc.CreateElement("ZJHM");
                childNode.InnerText = dr["ZJHM"].ToString();
                houseCheckNode.AppendChild(childNode);
                element.AppendChild(houseCheckNode);
            }
            //将xml文档输出
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            doc.Save(ms);
            string filename = this.GetFileName();
            filename = Server.UrlEncode(filename);
            Response.ContentEncoding = Encoding.GetEncoding("utf-8");
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            Response.AddHeader("Content-Length", ms.Length.ToString());
            Response.ContentType = "application/octet-stream";
            byte[] b = ms.ToArray();
            Response.OutputStream.Write(b, 0, b.Length);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
            HttpContext.Current.ApplicationInstance.CompleteRequest();

            //string filePath = "你自己的路径"; 
            //FileInfo DownloadFile = new FileInfo(filePath); 
            //HttpContext.Current.Response.Clear();
            //HttpContext.Current.Response.ContentType = "application/octet-stream";
            //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, Encoding.UTF8));
            //HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); 
            //HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
            //HttpContext.Current.Response.Flush();
            //HttpContext.Current.Response.End(); 

        }
        else
        {
            dtlist.InnerHtml = " 查验错误!";
        }
    }

你可能感兴趣的:(C#ASPX)