Display Byte data (PDF) from Database in Browser using C# in ASP.Net MVC

Last Reply 14 days ago By ethirajram

Posted 14 days ago

Hi,

    i need to display var-binary data to PDF in MVC,

i saw your MVC pdf file display 

https://www.aspsnippets.com/Articles/Display-Show-PDF-file-embedded-in-View-in-ASPNet-MVC-Razor.aspx

it cover only physical file, but need to read varbinary data and display as PDF like your asp 

here is aspx hadnler model but not working in MVC

https://www.aspsnippets.com/Articles/Convert-Binary-data-to-PDF-file-in-C-and-VBNet.aspx

can you share how display varbinary dat to PDF in MVC

You are viewing reply posted by: ethirajram 14 days ago.
Posted 14 days ago

Hi Dharmendr,

thank you for replay, now i got asnswer on your reference, i'm new to MVC and just 2nd day i'm working that's why little bit confussed..

here is that code:

        public ActionResult DisplayPDF()
        {
            byte[] byteArray = GetPdfFromDB(4);
            MemoryStream pdfStream = new MemoryStream();
            pdfStream.Write(byteArray, 0, byteArray.Length);
            pdfStream.Position = 0;
            return new FileStreamResult(pdfStream, "application/pdf");
        }

        private byte[] GetPdfFromDB(int id)
        {
            #region
            byte[] bytes = { };
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT Scan_Pdf_File FROM PWF_InvoiceMain WHERE InvoiceID=@Id and Enabled = 1";
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        if (sdr.HasRows == true)
                        {
                            sdr.Read();
                            bytes = (byte[])sdr["Scan_Pdf_File"];
                        }
                    }
                    con.Close();
                }
            }

            return bytes;
            #endregion
        }
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html