0

I have a controller Action which is returning a RDLC report through return File() function. The output is ok when calling this Action through browser address bar. Again, when calling this action thorough ajax GET method then the success data is bellowed string. I just want to show output of ajax call in a new window as a PDF file. Is there a way? I do not want to send the the parameters as query string of window.open() function for calling the Action from browser address bar.

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
Muhammad Ashikuzzaman
  • 3,075
  • 6
  • 29
  • 53
  • 2
    You cannot download a file using ajax. Refer [Download Excel file via AJAX MVC](https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc) –  Aug 27 '18 at 05:40
  • Use redirection to a GET action method which returns the file. You can pass all required parameters and create the report inside that action method. – Tetsuya Yamamoto Aug 27 '18 at 05:47
  • @TetsuyaYamamoto I have done in this way as Stephen Muecke link said my desired way is not possible. – Muhammad Ashikuzzaman Aug 28 '18 at 02:53

1 Answers1

0

you must instead of window.open() use this

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

You can easily put codes in this action

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);

    }