如何将pdf文件结果返回给ajax jquery并将其显示在浏览器

时间:2015-10-31 04:26:37

标签: c# asp.net-mvc-3

我想使用rdlc生成pdf。我想用ajax调用它,它返回正确的数据,但我不知道如何在浏览器中显示它。

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

但是当我在按钮点击时从ajax调用此函数时,

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

警报(数据);显示unicode字符,但我想在浏览器的新选项卡中打开pdf文件。 我希望这个按钮点击就像当我点击按钮时,它应该转到ajax中指定的url,在该动作上我调用另一个返回文件的函数。

0 个答案:

没有答案