我有这个函数用于生成一个xml文件..问题是当我下载生成的文件时,我的xml格式正确,但在这个结尾也是我点击的页面的html源代码用于生成此按钮。问题是什么?这里是源代码:
protected void lnkEsporta_Click(object sender, EventArgs e)
{
List<Eventi> lista_eventi = //load list
string filename = "~/data/eventi.xml";
XmlTextWriter tw = new XmlTextWriter(Server.MapPath(filename), Encoding.GetEncoding("ISO-8859-1"));
tw.Flush();
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("wrapper");
tw.WriteElementString("project", GetDao().Get<Progetti, Int32>(lista_eventi[0].idprogetto).nome);
tw.WriteStartElement("events");
foreach (Eventi evento in lista_eventi)
{
tw.WriteStartElement("event");
tw.WriteElementString("idEv", evento.idev.ToString());
tw.WriteElementString("data", evento.DataInizio.ToString());
tw.WriteElementString("sede", evento.sede.ToString());
tw.WriteElementString("location", evento.location.ToString());
tw.WriteElementString("video", evento.video ? "True" : "False");
tw.WriteElementString("tutors", evento.tutors.ToString());
tw.WriteElementString("technicians", evento.technicians.ToString());
tw.WriteStartElement("aule");
List<Aule> lista_aule = //load list
foreach (Aule aula in lista_aule)
{
tw.WriteElementString("aula", aula.descrizione.ToString(), aula.idaula.ToString());
}
tw.WriteEndElement();
tw.WriteEndElement(); //end of source event
}
tw.WriteEndElement();//end of source events
tw.WriteEndElement();//end of source wrapper
tw.WriteEndDocument();//end of source tag
tw.Close();
Response.ClearContent();
//Response.End();
Response.Clear();
Response.StatusCode = 200;
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.AddHeader("Content-Transfer-Encoding", "binary");
//Response.AddHeader("Content-Length", _Buffer.Length.ToString());
Response.ContentType = "application-download";
Response.TransmitFile(Server.MapPath(filename));
}
我的开头是我的xml,之后它以页面源代码开头......我该怎么办?非常感谢你的帮助!
答案 0 :(得分:0)
我建议你错过Response.End()
。
另见Filename and mime problems - ASP.NET Download file (C#)
Response.ContentType = "application/xml";
Response.AddHeader("Content-Disposition", String.Format("inline; filename={0}.xml", Path.GetFileName(Path)));
Response.WriteFile(path); //
Response.Flush();
// Response.Close(); // end should do this anyway so optional
Response.End(); // end forces the server to send the request immediately. Failing to