如何在asp.net MVC中使用ajax打开PDF报告(从报告查看器) [英] How to open PDF report (from report viewer) using ajax in asp.net mvc

查看:28
本文介绍了如何在asp.net MVC中使用ajax打开PDF报告(从报告查看器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序(rdlc报告)中打印PDF文件.因此,我需要在浏览器中显示文件而不使用Ajax下载.以下代码正在尝试使用< a> tag(< a href =〜/.../" target ="_ blank"> print</a> ),但在ajax调用中不起作用.请使用C#提供代码示例或说明.

I want to print a PDF File from my application (rdlc report). So I need to display the file in the browser without downloading using Ajax. The following code is trying to open the file using <a>tag(<a href="~/.../" target="_blank">print </a>) but it does not work in the ajax call. Please provide a code sample or instruction in C#.

这是从报表查看器生成的PDF:

Here is the generation of the PDF from the report viewer:

public void CreatePdfFromList(string fileName, string reportpath, ArrayList dsNameArray, ArrayList DTArray)
{
    ReportViewer viewer = new ReportViewer();
    Warning[] warnings = null;
    string[] streamIds = null;
    string mimeType = string.Empty;
    string encoding = string.Empty;
    string extension = string.Empty;
    string filetype = string.Empty;

    viewer.SizeToReportContent = true;
    viewer.LocalReport.ReportPath = reportpath;
    viewer.ProcessingMode = ProcessingMode.Local;

    for (int i = 0; i < dsNameArray.Count; i++)
    {
        ReportDataSource rds1 = new ReportDataSource((string)dsNameArray[i], (IEnumerable)DTArray[i]);
        viewer.LocalReport.DataSources.Add(rds1);
    }

    viewer.LocalReport.Refresh();

    byte[] pdfContent = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

    Response.Buffer = true;
    Response.Clear();
    Response.ContentType = mimeType;
    //Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
    Response.BinaryWrite(pdfContent);
    //Response.Flush(); // send it to the client to download
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.End();
}

这是ajax:

$.ajax({
        type: "GET",
        url: "/.../",
         data: {...},
         success: function (json) {
         //window.open(json);
         //open file i think...
         }
         })

当尝试显示pdf内容时,我使用console.log(json).它显示了字符串,但内容显示如下:

When trying to show pdf content I use console.log(json). It shows me the string but the content appears like this:

�~�

推荐答案

在Ajax中

function openpdf()
    {
        $.ajax({
            url: '@Url.Action("OpenPDF", "TrangChu")',
            type: "GET",
            success: function (data) {
                 let pdfWindow = window.open("");
            pdfWindow.document.write("<iframe width='100%' height='100%' src='data:application/pdf;base64," + data + "'></iframe>");

            }
        });
    }

在视图中

  <button onclick="openpdf()">Open PDF</button>

在控制器中

 string strAttachment = Server.MapPath(@"/Reports/haha.pdf");
 byte[] renderdByte = lr.Render("Pdf", "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
             string base64EncodedPDF = System.Convert.ToBase64String(renderdByte);
        return Json(base64EncodedPDF, JsonRequestBehavior.AllowGet);

这篇关于如何在asp.net MVC中使用ajax打开PDF报告(从报告查看器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆