Monday, November 5, 2012

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



            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;

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...