如何在MVC中将PDF显示为Pop?

时间:2019-02-13 12:28:06

标签: c# asp.net-mvc

我正在尝试从数据库中获取pdf字节数据,并在我的视图中显示为

Partialview

<iframe id="PdfDisplay" style="width:100%;height:80vh" onload=""></iframe>

控制器动作

public ActionResult PDFDisplay(int id)
    {
        byte[] pdfByte = db.MasterCopyAPIs.Where(w => w.Id == id).Select(w => w.mastercopy).FirstOrDefault();
        //System.IO.File.WriteAllBytes(Server.MapPath(@"~/Files/" + TempData["fileName"].ToString()),pdfByte);
        return File(pdfByte, "application/pdf");
    }

ajax呼叫

 function DisplayMaster(id) {
    $.ajax({
        url: '@Url.Action("PDFDisplay", "ProductAPI")/?id=' + id,
        type: 'POST',
        cache: false,
        success: function (data, status, txt) {
            alert(data);
            $("#PdfDisplay").val(data);
            setTimeout(function () {
                window.location.replace('@Url.Action("Index", "ProductAPI")');//Need To Specify which window to redirect
                return true;
            });
        }
    });
}

我的通话视图

 <a href="" onclick="DisplayMaster(@Model.Id)">@Model.Mastercopyname</a>

1 个答案:

答案 0 :(得分:0)

         public ActionResult PDF(string Report)
        localReport.DataSources.Add(reportDataSource);
        localReport.DataSources.Add(reportDataSource2);
        string reportType = Report;
        string mimeType;
        string encoding;
        string fileNameExtension;
        if (reportType == "PDF")
        {
            fileNameExtension = "pdf";
        }
        else if (reportType == "EXCEL")
        {
            fileNameExtension = "xlsx";
        }
        else if (reportType == "WORD")
        {
            fileNameExtension = "docx";
        }
        Warning[] warnings;
        string[] streams;
        byte[] renderedBytes;
        renderedBytes = localReport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
        return File(renderedBytes, mimeType);