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

Last Reply 6 months ago By ethirajram

Posted 6 months ago


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

i saw your MVC pdf file display 


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


can you share how display varbinary dat to PDF in MVC

You are viewing reply posted by: ethirajram 6 months ago.
Posted 6 months 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)
            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;
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                        if (sdr.HasRows == true)
                            bytes = (byte[])sdr["Scan_Pdf_File"];

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