Base solution for your next web application
Open Closed

Pdf & xml download #3316


User avatar
0
onecontact created

Hi!, can someone help me to do this?: I want to download a pdf and xml files on the fly, we have the files stored in db as base64 encoded strings, we try with blob feature but not work in mobile, you have any recommendation?, we create a controller on the webapi project for response the files as bytes[]

here is the example code:

[HttpGet]
        public async Task<HttpResponseMessage> GetPdfInvoice(int saleId)
        {
            var fileName = string.Concat($@"invoice.pdf");
            var filePath = HttpContext.Current.Server.MapPath("~/Reporting/" + fileName);

            var nameFile = await GenerateNewPdf(saleId, filePath);

            var result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
            result.Content.Headers.ContentDisposition =
                new ContentDispositionHeaderValue("attachment") {FileName = nameFile };

            return result;
        }

        private Task<string> GenerateNewPdf(int saleId, string filePath)
        {
            return Task.Run(() =>
            {
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Report))
                {
                    var factura = _salesAppService.GetSale(saleId);
                    var fac = _clientAppService.GetClientbyId(factura.ClientId);
                    var lista = new List<SalesDto> {factura};
                    var client = new List<ClientDto> { fac };
                    var viewer = new ReportViewer();
                    viewer.LocalReport.EnableExternalImages = true;
                    viewer.LocalReport.LoadReportDefinition(stream);

                    Warning[] warnings;
                    string[] streamids;
                    string mimeType;
                    string encoding;
                    string filenameExtension;

                    viewer.LocalReport.DataSources.Add(new ReportDataSource("Factura", lista));
                    viewer.LocalReport.DataSources.Add(new ReportDataSource("Items", lista[0].SoldItems));
                    viewer.LocalReport.DataSources.Add(new ReportDataSource("Client",client));

                    viewer.LocalReport.Refresh();

                    var bytes = viewer.LocalReport.Render(
                        "PDF", null, out mimeType, out encoding, out filenameExtension,
                        out streamids, out warnings);

                    using (var fs = new FileStream(filePath, FileMode.Create))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
                    return factura.NameFile;
                }
            });
        }

someone can help me please to avoid create the pdf starting with a filepath?,(want to create in the fly), and make this working on mobile?, so many thanks


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Do you use ASP.NET Core or ASP.NET MVC ? IF you are using ASP.NET Core, you can directly use controllers and return

    return File(bytes, MimeTypeNames.ApplicationPdf);
    
  • User Avatar
    0
    onecontact created

    Hi, we are working with mvc 5.x + angular 1, now are using the controllers on the webapi, and mvc controllers, that it's ok?, we maybe can have any security trouble?, please advice.

    thank you

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    now are using the controllers on the webapi, and mvc controllers, that it's ok?

    No, this is not a problem. ABP provides application services as web api but you can always use regular webapi controllers when you need custom behaviour or something ABP does not cover.

    So, is this solved your main problem ?

    Thanks.

  • User Avatar
    0
    onecontact created

    Yes, so many thank's

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)