How to use ASP.net to save uploaded pictures to the database

Source: Internet
Author: User
Tags array object contains count sql prepare

In this article we can learn from the following aspects of knowledge:

1. Necessary conditions for inserting pictures

2. Using streaming objects

3. Find the size and type of pictures you want to upload

4. How to use the InputStream method?


necessary conditions for inserting pictures

Before we start uploading, there are two important things we need to do:

The enctype attribute of the #form tag should be set to Enctype= "Multipart/form-data"

# requires a <input type=file> form to enable the user to select the files they want to upload, and we need to import the System.IO namespace to process the stream objects

Apply the above three points to the ASPX page. At the same time we need to prepare the following for SQL Server.

# tables that require a field with at least one picture type

# If we have another field with a variable character type to store the picture type, that would be better.

Now we have a SQL table (a field that contains an image data type) and a <input type=file> tag. Of course we have to prepare the submit button so that the user submits it after selecting the picture. In the OnClick event of this button, we need to read the contents of the selected picture and then save it in the table. Let's take a look at this onclick event first.

Code for the OnClick event of the 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 does this work?
Personimage is the object of the HtmlInputFile control. First you need to get the size of the picture, you can use the following code to implement:

Intimagesize = PersonImage.PostedFile.ContentLength

Then return the type of the picture using the Contentype property. The last and most important thing is to get the image Stream, which can be implemented in the following code:

ImageStream = PersonImage.PostedFile.InputStream

We need an array of bytes to store the image content. Reading the entire picture can be implemented using the Read method of the Stream object. The Read (in byte[] buffer,int offset,int count) method has three parameters. "The Read method can be detailed in the. Net frameworksdk" They are:

Buffer

An array of bytes. When this method returns, the buffer contains the specified character array, and the value between offset and (offset + count) of the array is replaced by the byte read from the current source.

Offset

The zero-based byte offset in buffer at which to begin storing data read from the current stream.

Count

The maximum number of bytes to read from the current stream.

This read method is implemented with the following code:
Intstatus = Imagestream.read (imagecontent, 0, Intimagesize)
.

Now that we've read the entire picture, the next step is to save the content to the SQL table. We'll use stored procedures to complete inserting picture types and picture contents into SQL tables. If you look at the above code, you will find that we are using the Sqldbtype.image data type (datatype). OK, we've done this and we've successfully deposited the images into SQL Server. Here's the ASPX page we wrote.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.