从javascript将xml数据发送到Ashx处理程序并在新窗口中显示响应

时间:2015-02-10 13:54:41

标签: javascript c# jquery asp.net pdf

我有一个asp.net ASHX处理程序,它接受xml数据作为输入并生成pdf文档并将其返回到响应,如下所示:

 public override void NowProcessTheRequest(XmlWriter xw)
    {
       var bytes = get pdf bytes from service based on input xml xw

       var fileName="test";
       context.Response.ContentType = "application/pdf";
       context.Response.Headers.Add("Content-Disposition",
       string.Format("inline; filename=report_{0}_{1}.pdf", fileName, DateTime.Now.ToString("MM_dd_yyyy")));
       context.Response.Write(bytes);
       context.Response.Flush();
  }

现在我想在javascript的新窗口中打开生成的pdf。

我无法使用" window.open"因为我不知道如何在体内传递xml。那么我有什么选择从javascript调用这个ashx处理程序并发布一个xml数据并在新窗口中打开响应pdf。?

1 个答案:

答案 0 :(得分:0)

echo xml输入?

var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><abc>123</abc>";

$.post("/echo/xml/", {xml:xml}, "xml")
.then(function(xml) {
    var res = $(xml).find(":root")
    , popup = window.open("", "popup", "width=600,height=400");
    popup.document.body.innerHTML = res.get(0).outerHTML;
    popup.focus();
    popup.print();   
});

jsfiddle http://jsfiddle.net/guest271314/trsvLmhm/

请参阅js window.open then print()