How to deposit the image into the database in ASP.NET

xiaoxiao2021-03-06  57

Introduction may have a lot, we urgently need to store the image into the database. In some applications, we may have some sensitive information, which will be easily stolen by certain users because of something stored in the file system. So these data cannot be stored in the file system. In this article, we will discuss how to store pictures into SQL2000. In this article we can learn the following knowledge: 1. Insert the necessary conditions for the picture 2. Using the stream object 3. Find the size and type 4 of the previously uploaded pictures. How to use the InputStream method? The necessary conditions for inserting images Before we start uploading, there are two important things we need to do: #Form tag encType properties should be set to encType = "multipart / form-data" # Need a form to allow users to select them to upload The file, and we need to import system.io namespace to process stream objects to apply the above three points to the ASPX page. At the same time, we need to prepare SQL Server. # Requires at least a table with a field of a picture type # If we have another variable character type of field to store the image type, it will be better. Now, we have a SQL table (including a field of an image data type), and tagging. Of course we have to prepare the Submit button so that the user is submitted after the image is selected. In the onclick event of this button, we need to read the contents of the selected image, then store it in the table. Then let's take a look at this onclick event.

Onclick event codes submit button: 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 is this work? PersonImage is an object of the HTMLInputFile control. First, you need to get the size of the picture, you can use the following code implementation: intimagesize = personimage.postedFile.contentLength then returns the type of image using the contentype property.

转载请注明原文地址:https://www.9cbs.com/read-121115.html

New Post(0)