Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVCWebAPI的新选项卡中打开呈现的RDL报告?_C#_Asp.net Mvc_Pdf_Rdl - Fatal编程技术网

C# 如何在MVCWebAPI的新选项卡中打开呈现的RDL报告?

C# 如何在MVCWebAPI的新选项卡中打开呈现的RDL报告?,c#,asp.net-mvc,pdf,rdl,C#,Asp.net Mvc,Pdf,Rdl,我刚刚知道如何将我的RDL报告呈现为pdf文件,但它当前位于临时文件夹中。下一步应该是在新窗口或选项卡中打开以供查看。我尝试了很多解决方案,但似乎都不管用 以下是我当前的代码: [HttpGet] [Route("printDCF")] public IHttpActionResult printDCF(int controlFormDetailID) { try { ApiResult apiResult

我刚刚知道如何将我的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;
        }
    }

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

[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 viewer中的下载图标时,文件名与window.url.createObjectURL生成的url相同。您可以为自己的文件分配文件名属性name@Elbert约翰·费利佩索里。但是我在哪里设置它呢?我可以使用文件路径字符串修改答案FileName get。我认为@Elbert John FelipeOh对你很有用,但我使用了window.URL.createObjectURL。请记住,我没有将文件保存在物理位置,因此没有文件路径,只有GUID url。