I am working on a project and I need to display a pdf in a new tab, I have a controller which gets the pdf from a reporting service as a 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");
I am using Ajax to call the controller:
@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>
}
And the javascript for the success method is:
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();
}
}
And this is what I am getting in the new tab:
Can someone please help me with this problem, and explain to me what I am doing wrong or what I am missing from the solution.
Thank you so much.