I have to return CustomActionResult from Jquery AJAX POST call which in turn returns byte array.where i want to show pdf in new tab using return data
I have tried as following
var params =
{
RPT_Id: $('#hdnRM_RPT_ID').val(), RPT_NAME: $('#hdnRM_REPORT_PATH').val(), _params: jsonobject
, HAS_LOGO: $('#hdnRM_HAS_LOGO').val(), Is_Multiple: $('#hdn_ISMULTIPLE').val(), RPT_PARAMS: jsonMultiReportObject
, Fetch_SP: $('#hdn_Fetch_SP').attr('data-fetchsp'), rpttype: rpttype
};
$.ajax({
url: "/Reports/ViewCrystalReport",
type: 'POST',
async: false,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(params),
cache: false, responseType: 'arraybuffer',
success: function (data) {
console.log(data);
if (data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
else
globalFunctions.showMessage("error", translatedResources.roleDeleteMsg);
},
error: function (data, xhr, status) {
globalFunctions.onFailure(data, xhr, status);
}
});
my control action as follows
[HttpPost]
public CrystalReportPdfResult ViewCrystalReport(int RPT_Id = 0, string RPT_NAME = "",string _params= "", Boolean HAS_LOGO = false, Boolean Is_Multiple = false,string RPT_PARAMS="",string Fetch_SP="",string rpttype="")
{
ReportDocument reportDocument = ProcessCrystalReport( RPT_Id , RPT_NAME , _params , HAS_LOGO , Is_Multiple , RPT_PARAMS, Fetch_SP , rpttype );
if (!string.IsNullOrEmpty(reportPath))
{
return new CrystalReportPdfResult(reportDocument);
}
return null;
}
where
CrystalReportPdfResult
is custom actionresult class which reads crystal report and converts to byte array as follows
public CrystalReportPdfResult(ReportDocument rd, ExportFormatType reportType = 0)
{
SetDBLogonForReport(rd);
rd.SetDatabaseLogon(Oasis_User, Oasis_Password, Oasis_Server, Oasis_DB);
if (reportType == 0)
{
_contentBytes = StreamToBytes(rd.ExportToStream(ExportFormatType.PortableDocFormat));
}
else
_contentBytes = StreamToBytes(rd.ExportToStream(reportType));
}
I am able to see the pdf in new tab but without any content.
Please kindly assist me in resolving the issue in opening pdf from ajax call in asp.net mvc..
Thanks..
What I have tried:
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);