I'm trying to generate Active Reports (v16) in a Web APi Controller, netcore 6.0. The result in the browser is always "failed" for both types. What am I doing wrong ?
[HttpGet("GetExcelReport")]
public async Task<ActionResult> GetExcelReportAsync(string type)
{
var stream = GenerateDoc(type, Response);
await stream.CopyToAsync(Response.Body);
return new OkResult();
}
internal System.IO.Stream GenerateDoc(string type, HttpResponse response)
{
using (var reportStream =
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("App.Reports.Reports.PageReport1.rdlx"
))
using (var reader = new System.IO.StreamReader(reportStream))
{
var rpt = new GrapeCity.ActiveReports.PageReport(reader);
IRenderingExtension ri = null;
if (type == "pdf")
{
ri = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
Response.ContentType = "application/pdf";
Response.Headers.Add("content-disposition", $"attachment; filename=sdfgsdfg-sdfgsdf.pdf");
}
else
{
ri = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
Response.ContentType = "application/vnd.ms-excel";
Response.Headers.Add("content-disposition", $"attachment; filename=sdfgsdfg-sdfgsdf.xls");
}
var output = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
rpt.Document.Render(ri, output);
return output.GetPrimaryStream().OpenStream();
}
}
CodePudding user response:
Assuming your thrid-part tool works well and you could get the stream successfully.
You could check Microsoft.AspNetCore.Mvc.ControllerBase
class and you could find all methods to return FileResult:
For your requirement,you could try this method
public virtual FileStreamResult File(Stream fileStream, string contentType, string? fileDownloadName)
I tried and the Result:
CodePudding user response:
I changed the way in which I return the file.
[HttpGet("GetExcelReport")]
public IActionResult GetExcelReportAsync()
{
Assembly assembly = System.Reflection.Assembly.GetEntryAssembly() ?? throw new ArgumentNullException(nameof(System.Reflection.Assembly));
using (var reportStream = assembly.GetManifestResourceStream("BFW.Reports.Reports.PageReport1.rdlx"))
if (reportStream != null)
{
using (var reader = new System.IO.StreamReader(reportStream))
{
var rpt = new GrapeCity.ActiveReports.PageReport(reader);
IRenderingExtension? re = null;
re = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
var output = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
rpt.Document.Render(re, output);
return File(output.GetPrimaryStream().OpenStream(), "application/octet-stream", "excelfile.xlsx");
}
}
return NotFound();
}