store an image in database
I have an asp page. In asp page I have an image control. There is an image within the image control. I want to save this image in database. In the database the particular field data type is 开发者_StackOverflow中文版image. How is it possible?
Check this article out: Save and Retrieve Images from the Database using ASP.NET 2.0 and ASP.NET 3.5
Example from the above article
First of all you need to convert your image into bytes like this:
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = imgUpload.PostedFile;
//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
Then you use imgByte
as value when you add to your database.
精彩评论