您的位置:首页 > 编程语言 > ASP

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

2014-12-23 08:07 323 查看
/// <summary>

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

/// </summary>

/// <returns>生成的文件名称</returns>

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 = "<DocumentElement></DocumentElement>";

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 = " <span style='color:red'>查验错误!</span>";

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: