Translation: Lu Shaowei (reproduced)
Introduction In many cases, we need to save the picture to the database. In some applications, some sensitive information cannot be stored in a file system because any picture stored on the file system is easily illegally obtained by the user. This article will discuss how to save pictures into the SQL Server database in ASP.NET. In this paper, we will learn about the following aspects: l Upload image files LEAR DETECTION LET LEAR SYSTEM MET Uploading image file requirements Before loading, we need two important things # form attributes of two important things #Form tags need to be set as follows: enctype = "multipart / form-data
# Provide an HTML control that allows users to select a picture file:
# Also refer to the system.io namespace to handle the three items described above to the ASPX page. There are some of the following requirements in SQL Server: # a table with at least one field type to image #, there is a field of a varchar type for storing the picture type, we have a type of image field type. Data table and one (HTML file control). We also need a submission button, and you can click on it after selecting a picture. In the button's onclick event we have to get the contents of the image file and eventually insert it into the data table. Let's take a look at the button's onclick event, which read the picture and insert the image into the data table.
ONCLICK event code for submitting buttons
Dim intImageSize As Int64Dim strImageType As StringDim ImageStream As Stream 'Gets the Size of the ImageintImageSize = PersonImage.PostedFile.ContentLength' Gets the Image TypestrImageType = PersonImage.PostedFile.ContentType 'Reads the ImageImageStream = PersonImage.PostedFile.InputStreamDim ImageContent (intImageSize) As ByteDim intStatus As IntegerintStatus = ImageStream.Read (ImageContent, 0, intImageSize) 'Create Instance of Connection and Command ObjectDim myConnection As New SqlConnection (ConfigurationSettings.AppSettings ( "ConnectionString")) Dim myCommand As New SqlCommand ( "sp_person_isp", myConnection)' Mark the Command as a SPROCmyCommand.CommandType = CommandType.StoredProcedure 'Add Parameters to SPROCDim prmPersonImage As New SqlParameter ( "@ PersonImage", SqlDbType.Image) prmPersonImage.Value = ImageContentmyCommand.Parameters.Add (prmPersonImage) Dim prmPersonImageType As New SqlParameter ( "@ PersonimageType ", SqldbType.varchar, 255) prmpersonimageType.Value = StrimageTypeMyCommand .Parameters.Add (prmPersonImageType) TrymyConnection.Open () myCommand.ExecuteNonQuery () myConnection.Close () Response.Write ( "New person successfully added!") Catch SQLexc As SqlExceptionResponse.Write ( "Insert Failed Error Details are:." & SQLEXC.TOSTRING ()) END TRY How does it work?
Object Personimage is an HTMLInputFile control. First we have to get the size of the picture, achieve:
Intimagesize = personimage.postedfile.contentLength
Then you want to get the picture type by the ContentYPE property. Finally, the most important thing is to get the picture file stream, achieved by the following method:
Imagestream = personimage.postedfile.inputStream
We have a BYTE array imageContent, ready to save the image content. The entire picture is read through the READ method of the Stream object, which has three parameters, namely:
# 被 复 图 内容 内容 目 目 位置 位置 位置 位置 位置
# Number of childs needed to be read
The reading declaration is as follows:
IntStatus = images (ImageContent, 0, IntimageSize)
Now we read the entire picture content. Then we need to insert the image into the SQL data table, we will use a stored procedure to insert the picture type and the image into the SQL data sheet. If you have seen the list of code above, you know that we set the data type to SqldbType.Image. In this way, we successfully saved the picture to the SQL Server database.
Example of output sample
Figure: Save the picture to the SQL Server database
Test the code below
Code download
Click here to download the ASPX page
Click here to download the stored procedure
to sum up
In this way, we have completed how to save pictures to the database. We are also ready to use the examples and stored procedures provided on the download section above. If you want to know how to read images from SQL Server, please refer to my article Retrieving Images from Sql Server In Asp .NET
Translator Note: Due to the limitations of the HTTP transport protocol, the size of the file can be uploaded in different environments. This is not a perfect solution for web applications to upload large files, just provide a relatively good method. I used this method to successfully upload 30m files in the LAN, but in another native system with lower system performance, up to 5M data. In addition, readers need to pay attention to it, to properly modify the machine.config and web.config files when uploading big files, as long as you open these files, you can know how appropriate modifications. I hope that everyone will refer to it.