Home > Net >  how to download jsreport without open using c# in .Net
how to download jsreport without open using c# in .Net

Time:07-22

I am Use JS Report for view. This code render the file and and Save in root directory. But I want file is directly download not open for view.

  var header = await _jsReportMVCService.RenderViewToStringAsync(HttpContext, RouteData, "Header", new { });
                var footer = await _jsReportMVCService.RenderViewToStringAsync(HttpContext, RouteData, "Footer", new { });
                HttpContext.JsReportFeature()
                     .Recipe(Recipe.ChromePdf)
                     .Configure((r) =>
                     {
                         //r.Options = new RenderOptions
                         //{
                         //    Timeout = 600000
                         //};
                         r.Template.Chrome = new Chrome
                         {
                             HeaderTemplate = header,
                             FooterTemplate = footer,
                             DisplayHeaderFooter = true,
                             MarginTop = "1cm",
                             MarginLeft = "2cm",
                             MarginBottom = "2cm",
                             MarginRight = "1.5cm",
                             Format = "A3"
                         };
                     });
                HttpContext.JsReportFeature().OnAfterRender((renderer) =>
                {
                    using (var file = System.IO.File.Open("EmployeesList.pdf", FileMode.Create))
                    {
                        renderer.Content.CopyTo(file);

                    }
                    renderer.Content.Seek(0, SeekOrigin.Begin);
                });

CodePudding user response:

You can download a JSReport like this

[MiddlewareFilter(typeof(JsReportPipeline))]
public IActionResult InvoiceDownload()
{
            HttpContext.JsReportFeature().Recipe(Recipe.PhantomPdf)
                .OnAfterRender((r) => HttpContext.Response.Headers["Content-Disposition"] = "attachment; filename=\"myReport.pdf\"");

            return View("Invoice", InvoiceModel.Example());
}

You can get help from the official asp.net core example.

Hope this helps you

  • Related