Click here to Skip to main content
14,699,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
           //var pdfWin= window.open(data, '');
        }
        else
            globalFunctions.showMessage("error", translatedResources.roleDeleteMsg);
    },
    error: function (data, xhr, status) {
        globalFunctions.onFailure(data, xhr, status); //Error Function
    }
});


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 );
            //var rpttype = Request.QueryString["rpttype"];
            //if (rpttype == "pdf")
            //{
            //    if (!string.IsNullOrEmpty(reportPath)) { return new CrystalReportPdfResult(reportDocument, ExportFormatType.PortableDocFormat); }
            //}
            //else if (rpttype == "excel")
            //{
            //    if (!string.IsNullOrEmpty(reportPath)) { return new CrystalReportPdfResult(reportDocument, ExportFormatType.Excel); }
            //}

            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);
                   //var pdfWin= window.open(data, '');
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900