【问题标题】:How to open a rendered RDL report in a new tab in MVC Web API?如何在 MVC Web API 的新选项卡中打开呈现的 RDL 报告?
【发布时间】:2018-02-12 10:13:25
【问题描述】:

我刚刚想出了如何将我的 RDL 报告呈现为 pdf 文件,但它目前位于临时文件夹中。下一步应该是在新窗口或选项卡中打开以进行查看。我尝试了许多解决方案,但似乎没有任何效果。

这是我当前的代码:

    [HttpGet]
    [Route("printDCF")]
    public IHttpActionResult printDCF(int controlFormDetailID)
    {
        try
        {
            ApiResult apiResult = new ApiResult();
            var reportData = ControllerLogic.GetReportData(controlFormDetailID);
            generateReport(reportData);

            return Ok(apiResult);
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }

    public System.Web.Mvc.FileContentResult generateReport(List<DocumentControlFormPrintResult> reportData)
    {
        string RptPath = HttpContext.Current.Server.MapPath("~/AngularViews/forms/dcf/report/DCF_Report.rdl");
        LocalReport rpt = new LocalReport();

        rpt.DataSources.Add(new ReportDataSource("DataSet1", reportData));

        rpt.ReportPath = RptPath;
        string filePath = System.IO.Path.GetTempFileName();
        Export(rpt, filePath);      
        rpt.Dispose();

        System.Web.Mvc.FileContentResult result = new System.Web.Mvc.FileContentResult(System.IO.File.ReadAllBytes(filePath), "application/pdf")
        {
            FileDownloadName = "dcf_print.pdf",
        };

        return result;
    }

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

【问题讨论】:

    标签: c# asp.net-mvc pdf rdl


    【解决方案1】:

    我想,我自己试过这段代码对我来说很好,它对你很有用

    [HttpGet]
        [Route("api/Data/OpenPDF")]
        public HttpResponseMessage OpenPDF()
        {
            var stream = new MemoryStream();
            string filePath = @"D:\PDF\pdf-test.pdf";
            using (FileStream fileStream = File.OpenRead(filePath))
            {
                stream = new MemoryStream();
                stream.SetLength(fileStream.Length);
                fileStream.Read(stream.GetBuffer(), 0, (int)fileStream.Length);
            }
            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(stream.ToArray())
            };
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(System.Net.Mime.DispositionTypeNames.Inline)
            {
                FileName = System.IO.Path.GetFileName(filePath)
            };
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            return result;
        }
    

    【讨论】:

    • 谢谢。文件名如何工作?当我在 pdf 查看器中单击下载图标时,文件名与 window.URL.createObjectURL 生成的 url 相同。
    • 文件名属性你可以分配你自己的文件名@Elbert John Felipe
    • 对不起。但是我在哪里设置呢?
    • 我可以修改答案,FileName 使用文件路径字符串获取。这对你很有用,我认为@Elbert John Felipe
    • 哦,但我使用了 window.URL.createObjectURL。请记住,我没有将文件保存在物理位置,因此没有文件路径,只有 GUID url。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多