我正在开发一个项目,我需要在新标签中显示pdf,我有一个控制器,它从报告服务获取pdf byte[]
。
var data = Search(search_info);
var stream = new MemoryStream(data, 0, data.Length, true, true);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "filename=\"\"" + "caseoverview" + ".pdf" + "");
Response.ContentType = MimeTypes.ApplicationPdf;
Response.OutputStream.Write(data1, 0, Convert.ToInt32(stream.Length));
Response.Flush();
return new FileStreamResult(stream, "application/pdf");
我正在使用Ajax来调用控制器:
@using (Ajax.BeginForm("Search", "Controller", null,
new AjaxOptions
{
HttpMethod = "post",
OnComplete = "OnCompleteMethod",
OnFailure = "OnFailtureMethod"
}))
{
<div id="query-filter" class="filters">
@Html.Partial("QueryFilter", Model)
</div>
<div class="row">
<div class="col-md-2 col-sm-6 col-md-offset-8">
<button class="btn btn-submit btn-block" type="submit">Find</button>
</div>
<div class="col-md-2 col-sm-6">
<button class="btn btn-submit btn-block" type="reset">Reset</button>
</div>
</div>
}
成功方法的javascript是:
function OnCompleteMethod(dataq, status) {
if (status === "success") {
var w = window.open("data:application/pdf, " + escape(dataq.responseText));
w.document.write(dataq.responseText);
w.document.close();
}
}
这就是我在新标签中获得的内容:
有人可以帮我解决这个问题,并向我解释我的错误或解决方案中缺少的内容。
非常感谢你。
答案 0 :(得分:0)
我用不同的方法解决问题,而不是使用Ajax调用我只做了这个javascript函数:
function reportPrinter() {
var myParamether= $('#paramether').val();
window.open('/MyController/ReportPrinterMethod?report=reportview¶mether=' + myParamether, 'ReportView', '');
}
在Controller中我改变了这种方法的方法:
[HttpGet]
public ActionResult ReportPrinterMethod(string myParamether)
{
if (!ModelState.IsValid) return View();
var data = GetDataReport(myParamether); //Return a byte[] with the pdf content.
Response.AddHeader("Content-Disposition", "filename=\"\"" + "reportview" + ".pdf" + "");
Response.ContentType = "application/pdf";
Response.OutputStream.Write(data, 0, Convert.ToInt32(data.Length));
Response.Flush();
Response.Close();
return View();
}
这解决了这个问题。