asp.net|sql| upload | upload pictures | data | database
One, the picture into the database
Use the following knowledge in several aspects:
1. Using streaming objects
2. Find the size and type of pictures you want to upload
3. How to use the InputStream method
necessary conditions for inserting pictures
The enctype attribute of the 1.#form tag should be set to Enctype= "Multipart/form-data"
2.# needs a <input type=file> form to enable users to select the files they want to upload, and we need to import System.IO namespaces to process the stream objects
Make the following preparations for SQL Server
1.# a table that requires a field with at least one picture type
2.# If we had another field with a variable character type to store the picture type, that would be better.
form controls
1. Insert the picture to use is the System.Web.UI.HtmlControls.HtmlInputFile control, we put this control in the WebForm, named "Imginput"
2. At the same time put a confirmation upload button "Button1"
Program code
Addimg, for returning the content of the picture to be uploaded
1Private function addimg () function addimg (ByVal inputimg as System.Web.UI.HtmlControls.HtmlInputFile, ByVal Imgtype as String, ByVal MaxSize as Int64) as Byte ()
2 ' Incoming a HtmlInputFile control, an upload picture format and a upload image maximum, return the content of the picture, both write to the contents of the database, you can also write the picture type
3 Dim intimagesize as Int64
4 Dim Strimagetype as String
5 Dim ImageStream as Stream
6 ' Gets the Image Type
7 Strimagetype=inputimg.postedfile.contenttype
8 If strimagetype <> imgtype Then
9 Response.Write ("<script>alert (' picture type") </script> ") ' JGP type is ' Image/pjpeg '
the Exit Function
one end If
' Gets The Size of the Image
intimagesize = InputImg.PostedFile.ContentLength
If intimagesize > MaxSize Then
Response.Write ("<script>alert (' picture not greater than K ') </script>")
Exit Function
End If
' reads the Image
ImageStream = InputImg.PostedFile.InputStream
Dim imagecontent (intimagesize) as Byte
Dim Intstatus as Integer
intstatus = imagestream.read (imagecontent, 0, Intimagesize)
return imagecontent
End Function
The
example calls the
Dim imagecontent () as Byte
Imagecontent = addimg (fileimg, "Image/pjpeg", 512000) ' Upload image type jpg, max no more than 500K
Insert Database
I don't think that's something to write about, you can do it in any way (it's recommended to use stored procedures) and insert imagecontent into a field in the database with the type image.
Second, the picture from the database read out
This section is relatively simple:
Suppose an IMG variable is a picture that you take out of the database
So direct use
Response.BinaryWrite (IMG)
You can output the picture to the page.
Third: summary
Storing pictures in a database actually plays a role in the protection of the image, so that even if someone browses your machine and doesn't see your pictures, it can also be used to protect important picture materials.