Save image to database

I have an asp page. On the asp page, I have image control. Inside the image control there is an image. I want to save this image in a database. In a database, a particular field data type is an image. How is this possible?

+3
source share
1 answer

Check out this article: Save and retrieve images from a database using ASP.NET 2.0 and ASP.NET 3.5

Example from the above article

First of all, you need to convert the image to bytes as follows:

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 add imgByteas the value when adding to your database.

+4
source

Source: https://habr.com/ru/post/1792457/


All Articles