angularjs Web API和报告查看器

ddrv8njm  于 2022-10-31  发布在  Angular
关注(0)|答案(2)|浏览(171)

我需要在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");
    }
whitzsjs

whitzsjs1#

请检查以下内容:How to return PDF to browser in MVC?
我没有看到任何地方,你正在刷新流和设置位置为0。如果你的PDF文件是空的,那么你需要设置位置为0,以便数据有一个字节流的开始位置。

pokxtpni

pokxtpni2#

你可以试试这个。

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

相关问题