locked
View a pdf byte array in ListView or you can download it in pdf RRS feed

  • Question

  • User-733224187 posted

    Hello everyone, you need help viewing or downloading byte array from my SQL Server database in an asp.net application

    Model: 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Web;
    
    namespace mia.Models
    {
        public class Nfe_Omie
        {
            [Key]
            public int Omie_Id { get; set; }
    
          
            public string xml { get; set; }
    
            public string Num { get; set; }
    
            public string Serie { get; set; }
    
            public string Chave_Acesso { get; set; }
    
            public string Emitente_Nome { get; set; }
    
            public string Emitente_CNPJ { get; set; }
    
            public string Destinario_Nome { get; set; }
    
            public string Destinario_CNPJ { get; set; }
    
            public string Destinario_CPF { get; set; }
    
            
            public byte[] Danfe { get; set; }
    
    
    
            public ICollection<ProdutosNFeOmie> produtosNFeOmie { get; set; }
        }
    }

    The process is automatic for saving to the database, as there are more than 2000 thousand xmls files.
    Process:

     public static void CheckNFebyOmie()
            {
    
                DirectoryInfo Dir = new DirectoryInfo(@"C:\XML");
                // Busca automaticamente todos os arquivos em todos os subdiretórios
                FileInfo[] Files = Dir.GetFiles("*", SearchOption.AllDirectories);
    
                foreach (FileInfo File in Files)
                {
                    Nfe_Omie nfe_Omie = new Nfe_Omie();
                    ProdutosNFeOmie produtosOmie = new ProdutosNFeOmie();
    
                    
    
    
    
                    XmlDocument doc = new XmlDocument();
                    doc.Load(File.FullName);
    
    
                    
                    
                    XmlNodeList ide = doc.GetElementsByTagName("ide");
    
                    foreach (XmlElement nodo in ide)
                    {
                        nfe_Omie.Num = nodo.GetElementsByTagName("nNF")[0].InnerText.Trim();
                        nfe_Omie.Serie = nodo.GetElementsByTagName("serie")[0].InnerText.Trim();
                        produtosOmie.Produto_num_NFe = nodo.GetElementsByTagName("nNF")[0].InnerText.Trim();
                        produtosOmie.Produto_serie_NFe = nodo.GetElementsByTagName("serie")[0].InnerText.Trim();
                    }
    
                    var isExiste = NumeroSerie(nfe_Omie.Num, nfe_Omie.Serie);
    
                    if (!isExiste)
                    {
                        var modelo = DanfeViewModelCreator.CriarDeArquivoXml(File.FullName);
    
                        //gera  Danfe em Pdf
                        //using (var danfe = new Danfe(modelo))
                        //{
                        //    danfe.Gerar();
                        //    danfe.Salvar(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.ToString()) + File.Name + ".pdf");
                        //}
    
                        //Inicia o Danfe o obtem os Bytes do PDF Gerado
                        var pdfStream = new MemoryStream();
                        var danfe = new Danfe(modelo);
                        danfe.Gerar();
                        var bytesPdf = danfe.ObterPdfBytes(pdfStream);
                        nfe_Omie.Danfe = bytesPdf;
                        
    
    
                        nfe_Omie.xml = File.ToString();
                        
    
                        XmlNodeList emit = doc.GetElementsByTagName("emit");
    
                        foreach (XmlElement nodo in emit)
                        {
                            nfe_Omie.Emitente_CNPJ = nodo.GetElementsByTagName("CNPJ")[0].InnerText.Trim();
                            nfe_Omie.Emitente_Nome = nodo.GetElementsByTagName("xNome")[0].InnerText.Trim();
                        }
    
                        XmlNodeList dest = doc.GetElementsByTagName("dest");
    
                        XmlNodeList cnpj = ((XmlElement)dest[0]).GetElementsByTagName("CNPJ");
    
                        if (cnpj.Count.Equals(1))
                        {
    
                            nfe_Omie.Destinario_CNPJ = cnpj[0].InnerText.Trim();
                            foreach (XmlElement nodo in dest)
                            {
                                nfe_Omie.Destinario_Nome = nodo.GetElementsByTagName("xNome")[0].InnerText.Trim();
                            }
                        }
                        else
                        {
                            foreach (XmlElement nodo in dest)
                            {
                                nfe_Omie.Destinario_CPF = nodo.GetElementsByTagName("CPF")[0].InnerText.Trim();
                                nfe_Omie.Destinario_Nome = nodo.GetElementsByTagName("xNome")[0].InnerText.Trim();
                            }
                        }
    
                        db.Nfe_Omie.Add(nfe_Omie);
                        db.SaveChanges();
    
                        XmlNodeList prod = doc.GetElementsByTagName("prod");
    
    
                        foreach (XmlElement nodo in prod)
                        {
    
                            produtosOmie.Produto_Cod = nodo.GetElementsByTagName("cProd")[0].InnerText.Trim();
                            produtosOmie.Produto_Desc = nodo.GetElementsByTagName("xProd")[0].InnerText.Trim();
                            produtosOmie.Produto_Unidade = nodo.GetElementsByTagName("uCom")[0].InnerText.Trim();
                            produtosOmie.Produto_Quant = nodo.GetElementsByTagName("qCom")[0].InnerText.Trim();
                            produtosOmie.Produto_ValorUnidade = nodo.GetElementsByTagName("vUnCom")[0].InnerText.Trim();
                            produtosOmie.Produto_Valor = nodo.GetElementsByTagName("vProd")[0].InnerText.Trim();
                            produtosOmie.nfe_Omie = new List<Nfe_Omie>();
                            
                            var numserie = db.Nfe_Omie.Where(n => n.Num == produtosOmie.Produto_num_NFe && n.Serie == produtosOmie.Produto_serie_NFe).FirstOrDefault();
    
                            if (numserie != null)
                            {
    
                                produtosOmie.nfe_Omie.Add(numserie);
    
                            }
                           
                            db.ProdutosNFeOmie.Add(produtosOmie);
                            db.SaveChanges();
    
                        }
    
                    }
                }
            }

    Tuesday, November 5, 2019 1:43 PM

Answers

  • User-733224187 posted

    Hello jiadongm, I modified my idea of ​​showing the PDF, I save the Xml in the DB and then using FileResultI generate it on another tab.

     [HttpGet]
            public FileResult PDFDownload(int id)
            {
                Nfe_Omie nfe_Omie = db.Nfe_Omie.Find(id);
                //Metodo para gerar a dafe do XML
                var modelo = DanfeViewModelCreator.CriarDeStringXml(nfe_Omie.xml);
              
                //Inicia o Danfe o obtem os Bytes do PDF Gerado
    
                var pdfStream = new MemoryStream();
                var danfe = new Danfe(modelo);
                danfe.Gerar();
                var bytesPdf = danfe.ObterPdfBytes(pdfStream);
               
    
                return File(bytesPdf, "application/pdf");
            }

    Link to open new tab

      @Html.ActionLink("PDF", "PDFDownload", new { id = item.Omie_Id }, new { @class = "has-load btn btn-outline-info Center", target = "_blank" })

    You just need to generate the blank view.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2019 6:26 PM

All replies

  • User-474980206 posted

    browsers do not directly support PDF. They count on an external application to render the PDF. the browser just passes the window to the PDF application that renders the PDF from the browser cache. For the pdf to not take the whole browser window, you use an iframe (which is typically another browser instance) to host the pdf.

    to download a pdf to view in the browser, its just an action that returns the PDF as a bytes stream with a content type.  the action url can be the "src" of an <iframe> or the "href" of an <a>.

    Tuesday, November 5, 2019 4:16 PM
  • User-733224187 posted

    Would you have an example of how to use this external application?

    Tuesday, November 5, 2019 4:45 PM
  • User-17257777 posted

    Hi ecocash,

    You can force the download of a PDF file, this link shows how to implement it, which may be helpful to you.

    https://www.c-sharpcorner.com/article/uploading-downloading-pdf-files-in-binary-format-using-fileresult-in-asp-net-mvc/

    Best Regards,

    Jiadong Meng

    Wednesday, November 6, 2019 5:48 AM
  • User-733224187 posted

    Hello jiadongm, I modified my idea of ​​showing the PDF, I save the Xml in the DB and then using FileResultI generate it on another tab.

     [HttpGet]
            public FileResult PDFDownload(int id)
            {
                Nfe_Omie nfe_Omie = db.Nfe_Omie.Find(id);
                //Metodo para gerar a dafe do XML
                var modelo = DanfeViewModelCreator.CriarDeStringXml(nfe_Omie.xml);
              
                //Inicia o Danfe o obtem os Bytes do PDF Gerado
    
                var pdfStream = new MemoryStream();
                var danfe = new Danfe(modelo);
                danfe.Gerar();
                var bytesPdf = danfe.ObterPdfBytes(pdfStream);
               
    
                return File(bytesPdf, "application/pdf");
            }

    Link to open new tab

      @Html.ActionLink("PDF", "PDFDownload", new { id = item.Omie_Id }, new { @class = "has-load btn btn-outline-info Center", target = "_blank" })

    You just need to generate the blank view.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2019 6:26 PM