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");
}
I do not know about the ReportViewer api, but if you are generating a file and would like to return this file as a response, you could do it simply like this(you needn't close the file stream as the web api takes care of closing it once it copies the content
of the file to the network):
public HttpResponseMessage GetFile()
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(File.OpenRead("c:\test.pdf"));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
pegz82
Member
9 Points
10 Posts
Web API and report viewer
Nov 19, 2012 10:38 AM|LINK
Hi
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"); }Thanks
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Web API and report viewer
Nov 19, 2012 08:58 PM|LINK
I do not know about the ReportViewer api, but if you are generating a file and would like to return this file as a response, you could do it simply like this(you needn't close the file stream as the web api takes care of closing it once it copies the content of the file to the network):
public HttpResponseMessage GetFile() { HttpResponseMessage response = new HttpResponseMessage(); response.Content = new StreamContent(File.OpenRead("c:\test.pdf")); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); return response; }Kiran Challa