Introduction In many cases, we need to save images to the database. In some applications, there is some sensitive information that cannot be stored in the file system, because any images stored on the file system can easily be illegally obtained by the user.
This article will discuss how to save images to a SQL SERVER database in ASP.NET.
In this article we will learn about the following aspects:
l Requirements for uploading image files
l Using Strem objects
l Get uploaded image size and type
l How to use the InputStream method?
Requirements for Uploading Image Files Before we start uploading we need to do two important things
The enctype attribute of the #Form tag needs to be set to the following form:
enctype="multipart/form-data"
#Provide an Html control that allows users to select image files:
<input type=file>
#Also reference the System.IO namespace to process Strem objects. The above three items must be applied to aspx pages. There are also the following requirements in SQL SERVER:
#A table with at least one field type of Image
#In addition, it would be better to have a Varchar type field used to store image types. Then, we have a data table with an Image field type and an <input type=file> (HTML file control). We also need a submit button that the user can click after selecting the image. In the button's OnClick event we need to obtain the content of the image file and finally insert it into the data table. Let's take a look at the button's OnClick event, which reads the image and inserts it into the data table.
Submit button OnClick event code
http://blog.downcodes.com/
Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream
' Gets the Size of the Image
intImageSize = PersonImage.PostedFile.ContentLength
' Gets the Image Type
strImageType = PersonImage.PostedFile.ContentType
' Reads the Image
ImageStream = PersonImage.PostedFile.InputStream
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As New SqlCommand("sp_person_isp", myConnection)
' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
' Add Parameters to SPROC
Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)
prmPersonImage.Value = ImageContent
myCommand.Parameters.Add(prmPersonImage)
Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)
prmPersonImageType.Value = strImageType
myCommand.Parameters.Add(prmPersonImageType)
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Response.Write("New person successfully added!")
Catch SQLexc As SqlException
Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())
End Try
How does it work?
The object PersonImage is the HTMLInputFile control. First we need to get the size of the inserted image, which is achieved by the following method:
intImageSize = PersonImage.PostedFile.ContentLength
Next, you need to get the image type through the ContentType property. The last and most important thing is to obtain the image file stream, which is achieved through the following methods:
ImageStream = PersonImage.PostedFile.InputStream
We have a byte array ImageContent, which is ready to be used to save the image content. The entire image is read through the Read method of the Stream object. This method has three parameters, namely:
#The target location of the copied image content
#Start position of reading
#The subsection that needs to be read is declared as follows:
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
Now, we read the entire image content. Next we need to insert the image content into the SQL data table. We will use a stored procedure to insert the image type and image into the SQL data table. If you have seen the code listing above, you will know that we set the data type to SqlDbType.Image. In this way, we successfully saved the image to the SQL SERVER database.