Uploading Image to Sql Server Image Datatype in asp.net using fileupload Control

Uploading Image to Sql Server Image Datatype in asp.net using fileupload Control


 Byte[] imgbyte = null;
 if (FileUpload1.HasFile)
            {
                string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UserPhoto/") + DateTime.Now.ToString("_MMddyyyy_HHmmss") + fileName);
                photo = "UserPhoto/" + DateTime.Now.ToString("_MMddyyyy_HHmmss") + fileName;




                string fpathPhoto = Server.MapPath(photo);

                FileInfo fInfo = new FileInfo(fpathPhoto);
                long numBytes = fInfo.Length;

                //Open FileStream to read file
                FileStream fStream = new FileStream(fpathPhoto, FileMode.Open, FileAccess.Read);

                //Use BinaryReader to read file stream into byte array.
                BinaryReader br = new BinaryReader(fStream);

                //When you use BinaryReader, you need to supply number of bytes to read from file.
                //In this case we want to read entire file. So supplying total number of bytes.
                imgbyte = br.ReadBytes((int)numBytes);


            }

Comments

Popular posts from this blog

Get Running Sum of Query SQL Query