首页 > 解决方案 > Asp .Net Mvc:如何通过 Ajax 调用加载 RDLC 报告

问题描述

我有一个控制器 Action,它通过 return File() 函数返回 RDLC 报告。通过浏览器地址栏调用该Action时输出正常。同样,当通过 ajax GET 方法调用此操作时,成功数据是下面的字符串。我只想在新窗口中将 ajax 调用的输出显示为 PDF 文件。有办法吗?我不想将参数作为 window.open() 函数的查询字符串发送,以便从浏览器地址栏中调用 Action。

success: function (data) { 
     var w = window.open();
     $(w.document.body).html(data);
     }

%PDF-1.3 1 0 obj [/PDF /Text /ImageB /ImageC /ImageI] endobj 4 0 obj
<< /Length 123 /Filter /FlateDecode>> stream X }N� �0���M���\�籅v�8�� ��u> /Font
<< /F3 3 0 R>> >> >> endobj 3 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding>> endobj 5 0 obj
<< /Type /Pages /Kids [ 2 0 R ] /Count 1>> endobj 6 0 obj
<< /Type /Catalog /Pages 5 0 R>> endobj 7 0 obj
<< /Title /Author <> /Subject
<> /Creator (Microsoft Reporting Services 10.0.0.0) /Producer (Microsoft Reporting Services PDF Rendering Extension 10.0.0.0) /CreationDate (D:20180827103611+06'00') >> endobj xref 0 8 0000000000 65535 f 0000000010 00000 n 0000000266 00000 n
0000000429 00000 n 0000000065 00000 n 0000000529 00000 n 0000000591 00000 n 0000000643 00000 n trailer
<< /Size 8 /Root 6 0 R /Info 7 0 R>> startxref 957 %%EOF

标签: c#jqueryajaxasp.net-mvcrdlc

解决方案


你必须代替 window.open() 使用这个

    success: function (data) {
        var w = window.open();
        w.location = '/Cashier/CashDesk/Download';
        /*go to address get methon*/
    },

您可以轻松地将代码放入此操作中

public ActionResult Download()

    {

        LocalReport localReport = new LocalReport();
        localReport.ReportPath = Server.MapPath("~/Reports/PatientRecipt.rdlc");
        ReportDataSource reportDataSource = new ReportDataSource();
        reportDataSource.Name = "DataSet1";
        //reportDataSource.Name = "PatientRecipt";
        reportDataSource.Value = applicationDbContext.TblInvoices.Select(x => x).ToList();
        localReport.DataSources.Add(reportDataSource);
        string reporttype = "PDF";
        string mimeType;
        string encoding; string[] streams;
        Warning[] warnings;
        byte[] renderedByte;
        renderedByte = localReport.Render(reporttype, "", out mimeType, out encoding, out reporttype, out streams, out warnings);

        Response.AddHeader("content-disposition", "attachment;filename = expens_report." + reporttype);

        return File(renderedByte, reporttype);

    }

推荐阅读