2

I want to generate pdf using rdlc. I want to call it using ajax, it returns correct data but I dont know how to show it in browser.

public class CustomerController 
{
 //Render the report
 public FileResult PrintPOFILE(CustomerInputModel model)
    {
        //File generation logic

        renderedBytes = localReport.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);

        //FileStream fs = new FileStream(filePath, FileMode.Create);
        using (FileStream fs = new FileStream(Server.MapPath("~/download/") + FileName + ".pdf", FileMode.Create))
        {
            fs.Write(renderedBytes, 0, renderedBytes.Length);
        }

        Response.ClearHeaders();
        Response.ClearContent();
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "inline; filename=" + FileName + ".pdf;");

        Response.WriteFile(Server.MapPath("~/download/" + FileName + ".pdf"));
        Response.Flush();
        Response.Close();
        Response.End();

        string PdfUrl = Server.MapPath("~/download/" + FileName + ".pdf");
  return File(PdfUrl, "application/pdf");
    }

 [HttpPost]
    public ActionResult PrintFile(string Id)
    {
        int id = Convert.ToInt32(Id);            
        CustomerInputModel CustomerPrintModel = GetCustomer(id);           //function to get information of customer
        PrintPOFILE(CustomerPrintModel );
        return View(CustomerPrintModel );
    }

but when I call this function from ajax on button click,

  <p>
                <input type="button" value="Print PO" id="PrintBtn" name="button" />
            </p>
 $("#PrintBtn").click(function (){
 var id = $("#Id").val();
     $.ajax({
            url: "/Customer/PrintFile",
            type: "POST",
            data: {Id : id},
            success: function (data) 
            {
                alert(data);                        

                window.open("http://localhost:51035/download/"+data,"_blank");


            }
        });
      //  alert("Print");
    });
</script>

alert(data); shows unicode characters but I want pdf file to open in new tab of browser. I want this on button click like when I click the button, it should go to the url specified in ajax, on that action i called another function that returns file.

Monica
  • 141
  • 4
  • 16
  • Possible duplicate of [Download and open pdf file using Ajax](http://stackoverflow.com/questions/1999607/download-and-open-pdf-file-using-ajax) – venerik Oct 31 '15 at 08:30

0 Answers0