Web API和报告查看器 [英] Web API and report viewer

查看:54
本文介绍了Web API和报告查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Web API中提供一项服务,该服务将返回本地报告查看器文件的pdf.

I have a requirement to produce within a Web API a service that will return a pdf of a local report viewer file.

在MVC中,您可以使用FileResult做类似的事情,但是我正努力将其复制为HttpResponseMessage.有没有人尝试过或成功地做过类似的事情?我所有试图将byte []转换为流然后再输出为HttpResponse的尝试都以空文件结尾.

In MVC you can do something like this using FileResult but i'm struggling to replicate this as a HttpResponseMessage. Has anybody ever tried or had success in trying to do anything similar? All my attempts in trying toconvert the byte[] to a stream and then output as an HttpResponse have ended up with empty files.

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

推荐答案

您可以尝试一下..

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

这篇关于Web API和报告查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆