Save Image to Database as Binary using FileUpload in asp.net

Unknown | 1:51 AM |


 if (fupimage.HasFile)
            {
                //getting length of uploaded file
                int length = fupimage.PostedFile.ContentLength;
                //create a byte array to store the binary image data
                imgbyte = new byte[length];
                //store the currently selected file in memeory
                HttpPostedFile img = fupimage.PostedFile;
                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);
                string imagename = fupimage.PostedFile.FileName;
                //use the web.config to store the connection string            
            }
            else
            {        

                string fpath = Server.MapPath("~//images//face.gif");
                System.IO.FileInfo imageInfo = new System.IO.FileInfo(fpath);            
                imgbyte = new byte[imageInfo.Length];
                System.IO.FileStream imagestream = imageInfo.OpenRead();
                imagestream.Read(imgbyte, 0, imgbyte.Length);
                imagestream.Close();

            }


            cmd.Parameters.Add("@photo", SqlDbType.Image).Value = imgbyte;

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....