0

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

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");
    }
Amin Uddin
  • 968
  • 6
  • 15
  • 31

2 Answers2

0

Please check this out : How to return PDF to browser in MVC?

I dont see anywhere that you are flushing the stream and setting the position to 0. If your PDF file is empty, then you need to set the position to 0 in order the data to have a start position for the byte stream.

Community
  • 1
  • 1
Hozikimaru
  • 1,144
  • 1
  • 9
  • 20
  • Correct. It appears to me that LocalReport does not seem to have the flush method so what you can in fact do is, convert the streamBytest to a Stream and then return that after flushing it and setting the position as it is in the given URL. – Hozikimaru Oct 26 '14 at 15:46
0

You can try this..

                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")
                    };
coder100
  • 21
  • 3