我使用以下代码保存xml文件
HttpResponse response = HttpContext.Current.Response;
string xmlString = "<xml>blah</xml>";
string fileName = "ExportedForm.xml";
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.Write(xmlString);
例如,当绑定到按钮onlick事件时,这可以正常工作。但我在通用handler.ashx中生成我的xml。所以我想等到ajax调用完成后才能执行上面的操作。我可以在处理程序中完成所有操作吗? ajax调用由jquery进行,xml数据基于我页面上的输入字段。感谢
答案 0 :(得分:1)
我会用这样的东西。
using System;
using System.Web;
public class GenerateXML : IHttpHandler
{
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
var resp = context.Response;
resp.ContentType = "text/xml";
resp.ContentEncoding = System.Text.Encoding.UTF8;
resp.Write( "<xml>i am xml</xml>" );
}
}
然后修改项目的web.config。
<httpHandlers>
<!--...other things..-->
<add verb="*" path="xml.generate" type="YOUR_NAMESPACE.GenerateXML, YOUR_DLL" />
</httpHandlers>