Save/Remove pictures in asp.net/from SQL database (can be used to upload pictures)

Source: Internet
Author: User
Tags exit insert return variable
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 &lt;&gt; imgtype Then


9 Response.Write ("&lt;script&gt;alert (' picture type") &lt;/script&gt; ") ' JGP type is ' Image/pjpeg '


the Exit Function


one end If


' Gets The Size of the Image


intimagesize = InputImg.PostedFile.ContentLength


If intimagesize &gt; MaxSize Then


Response.Write ("&lt;script&gt;alert (' picture not greater than K ') &lt;/script&gt;")


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.







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.