Skip to content

Instantly share code, notes, and snippets.

@ChrisBriggsy
Last active August 29, 2015 14:19
Show Gist options
  • Save ChrisBriggsy/ca32c491c122eb71ce3b to your computer and use it in GitHub Desktop.
Save ChrisBriggsy/ca32c491c122eb71ce3b to your computer and use it in GitHub Desktop.
SaveReport
[HttpPost]
public HttpResponseMessage SaveReport(string input)
{
var reportDto = JsonConvert.DeserializeObject<ReportDTO>(input);
byte[] bytes;
using (var reportViewer = new ReportViewer())
{
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportEmbeddedResource = reportDto.ResourcePath;
if (reportDto.Parameters != null)
{
foreach (ParameterDTO parameter in reportDto.Parameters)
{
reportViewer.LocalReport.SetParameters(new ReportParameter(parameter.Name, parameter.Data));
}
}
if (reportDto.DataSources != null)
{
var dt = new DataTable();
foreach (DataSourceDto dataSource in reportDto.DataSources)
{
dt.Columns.Add(dataSource.Name, typeof (string));
}
DataRow dr = dt.NewRow();
for (int index = 0; index < reportDto.DataSources.Count; index++)
{
dr[index] = reportDto.DataSources[index].Data;
}
dt.Rows.Add(dr);
reportViewer.LocalReport.DataSources.Add(new ReportDataSource(reportDto.ReportDataSourceName, dt));
}
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
bytes = reportViewer.LocalReport.Render("Pdf", null, out mimeType, out encoding, out extension, out streamids, out warnings);
}
var cd = new System.Net.Mime.ContentDisposition
{
FileName = string.Format("SampleReport.pdf"),
Inline = true,
};
var result = new HttpResponseMessage(HttpStatusCode.OK);
Stream stream = new MemoryStream(bytes);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment