【问题标题】:Web API and report viewerWeb API 和报告查看器
【发布时间】:2014-12-21 14:08:23
【问题描述】:

我需要在 Web API 中生成一个服务,该服务将返回本地报告查看器文件的 pdf。

在 MVC 中,您可以使用 FileResult 执行类似的操作,但我正在努力将其复制为 HttpResponseMessage。有没有人尝试过或成功地尝试过类似的事情?我尝试将 byte[] 转换为流然后作为 HttpResponse 输出的所有尝试都以空文件告终。

public FileResult File() {
        // Create a new dataset
        StudentDataSet ds = new StudentDataSet();

        // Create and fill the Student data table
        // using the Student table adapter

        StudentDataSetTableAdapters.StudentTableAdapter dta =
               new StudentDataSetTableAdapters.StudentTableAdapter();
        dta.Fill(ds.Student);

        // Create a new report datasource with
        //      Name = the dataset name in the report,
        //      Value = the populated data table.

        ReportDataSource rds = new ReportDataSource();
        rds.Name = "DataSet1";
        rds.Value = ds.Student;

        ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
        rv.ProcessingMode = ProcessingMode.Local;
        rv.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");

        // Add the new report datasource to the report.
        rv.LocalReport.DataSources.Add(rds);

        rv.LocalReport.Refresh();

        byte[] streamBytes = null;
        string mimeType = "";
        string encoding = "";
        string filenameExtension = "";
        string[] streamids = null;
        Warning[] warnings = null;

        streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

        return File(streamBytes, mimeType, "StudentReport.pdf");
    }

【问题讨论】:

    标签: c# angularjs asp.net-web-api rdlc


    【解决方案1】:

    你可以试试这个..

                    byte[] ResponseStream = objConnection.GetStream(letterNumber, paperType);
                    HttpResponseMessage apiResponse;
                        apiResponse = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new ByteArrayContent(ResponseStream)
                        };
                        apiResponse.Content.Headers.ContentLength = ResponseStream.Length;
                        apiResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                        apiResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                        {
                            FileName = String.Format("Letter_" + letterNumber + ".pdf")
                        };
    

    【讨论】:

      【解决方案2】:

      请查看:How to return PDF to browser in MVC?

      我在任何地方都没有看到您正在刷新流并将位置设置为 0。如果您的 PDF 文件为空,那么您需要将位置设置为 0,以便数据具有字节流的起始位置。

      【讨论】:

      • 正确。在我看来,LocalReport 似乎没有 flush 方法,所以您实际上可以做的是,将 streamBytest 转换为 Stream,然后在刷新它并将位置设置为给定 URL 后返回它。跨度>
      猜你喜欢
      • 1970-01-01
      • 2014-07-08
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      相关资源
      最近更新 更多