1. store images in the database
Use the following knowledge:
1. Use stream objects
2. Search for the size and type of the image to be uploaded
3. How to Use the inputstream Method
Necessary Conditions for inserting an image
1. # enctype attribute marked by form should be set to enctype = "multipart/form-Data"
2. # A <input type = File> form is required to allow users to select the files they want to upload. At the same time, we need to import the system. Io namespace to process stream objects.
Prepare SQL Server as follows:
1. # a table that must contain at least one field of the image type
2. # It would be better if we had another character type field to store the image type.
Form Control
1. The system. Web. UI. htmlcontrols. htmlinputfile control is used to insert images. We put this control in webform and named it "imginput"
2. Put another confirmation upload button "button1"
ProgramCode
Addimg, used to return the content of the image to be uploaded
1 private function addimg () function addimg (byval inputimg as system. Web. UI. htmlcontrols. htmlinputfile, byval imgtype as string, byval maxsize as int64) as byte ()
2 'input an htmlinputfile control, an upload image format and a maximum value of the uploaded image. The returned image content must be written into the database, and you can also write the image 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 ('image type:" "') </SCRIPT>") 'jgp type: "image/pjpeg"
10 Exit Function
11 end if
12 'gets the size of the image
13 intimagesize = inputimg. postedfile. contentlength
14 if intimagesize> maxsize then
15 response. Write ("<SCRIPT> alert ('image cannot be greater than k') </SCRIPT> ")
16 exit function
17 end if
18 'reads the image
19 imagestream = inputimg. postedfile. inputstream
20 dim imagecontent (intimagesize) as byte
21 dim intstatus as integer
22 intstatus = imagestream. Read (imagecontent, 0, intimagesize)
23 return imagecontent
24 end Function
Example call
Dim imagecontent () as byte
Imagecontent = addimg (fileimg, "image/pjpeg", 512000) 'upload image type: JPG, up to 500 KB
Insert Database
I don't want to write this part anymore. You can use any method (stored procedure is recommended) to insert the imagecontent to the field of the image type in the database.
2. Read images from the database
This part is relatively simple:
Assume that the IMG variable is the image content you have extracted from the database.
Use it directly
Response. binarywrite (IMG)
You can output the image to the page.
Iii. Summary
storing images in the database actually protects images, so that even if someone browses your machine, they will not be able to see your images. This can also be used to protect important image materials.