在MVC WebApi

时间:2018-02-12 05:19:20

标签: asp.net-mvc pdf rdl

我正在尝试创建一个单击“打印”按钮时的打印功能。将打开一个新的选项卡或窗口,以PDF格式显示RDL文件。

我发现此链接可以帮助我提出一个主意:https://stackoverflow.com/a/40841394/8245979

但我在 FileResult 方法的返回中出错可能是因为我正在使用api。我认为它下载文件而不是仅打开它。有人可以帮我改写代码来完成我的任务吗?

    public FileResult ViewReport()
    {            
        string RptPath = Server.MapPath("~/AngularViews/forms/dcf/report/dcf.rdl");                  
        Microsoft.Reporting.WebForms.LocalReport rpt = new Microsoft.Reporting.WebForms.LocalReport(); 

        /* Bind Here Report Data Set */

        rpt.ReportPath = RptPath;
        string filePath = System.IO.Path.GetTempFileName();               
        Export(rpt, filePath);
        //CLOSE REPORT OBJECT           
        rpt.Dispose();
        return File(filePath, "application/pdf");
    } 




    public string Export(LocalReport rpt, string filePath){

    string ack = "";
       try {                
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;

        byte[] bytes = rpt.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
        using (FileStream stream = File.OpenWrite(filePath))
        {
            stream.Write(bytes, 0, bytes.Length);
        }
        return ack;
    }
    catch(Exception ex)
    {
        ack = ex.InnerException.Message;
        return ack; }           
   }

0 个答案:

没有答案