欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在ASP.net中保存/取出圖片入/從SQL數(shù)據(jù)庫(kù)

 更新時(shí)間:2006年09月28日 00:00:00   作者:  

一、把圖片存入數(shù)據(jù)庫(kù)中

用到以下幾個(gè)方面的知識(shí):
1. 使用流對(duì)象
2. 查找準(zhǔn)備上傳的圖片的大小和類型
3.怎么使用InputStream方法

插入圖片的必要條件
1.#Form 標(biāo)記的 enctype 屬性應(yīng)該設(shè)置成 enctype="multipart/form-data"
2.# 需要一個(gè)<input type=file>表單來(lái)使用戶選擇他們要上傳的文件,同時(shí)我們需要導(dǎo)入 System.IO名稱空間來(lái)處理流對(duì)象
對(duì)SqlServer做以下的準(zhǔn)備
1.# 需要至少含有一個(gè)圖片類型的字段的表
2.# 如果我們還有另外一個(gè)變字符類型的字段來(lái)存儲(chǔ)圖片類型,那樣會(huì)更好一些。

窗體控件
1.插入圖片用到的是System.Web.UI.HtmlControls.HtmlInputFile控件,我們?cè)趙ebform中放入這個(gè)控件,取名為“imgInput”
2.同時(shí)再放入一個(gè)確認(rèn)上傳按鈕“Button1”

程序代碼
AddImg,用于返回要上傳的圖片內(nèi)容

 1Private Function AddImg()Function AddImg(ByVal InputImg As System.Web.UI.HtmlControls.HtmlInputFile, ByVal ImgType As String, ByVal MaxSize As Int64) As Byte()
 2'傳入一個(gè)htmlinputfile控件,一個(gè)上傳圖片格式和一個(gè)上傳圖片最大值,返回圖片的內(nèi)容,既要寫(xiě)入數(shù)據(jù)庫(kù)中的內(nèi)容,你也可以同時(shí)寫(xiě)入圖片類型
 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('圖片類型為""')</script>") 'jgp類型為"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('圖片不得大于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
示例調(diào)用

Dim imageContent() As Byte
       imageContent = AddImg(fileImg, "image/pjpeg", 512000)'上傳圖片類型為jpg,最大不超過(guò)500K

插入數(shù)據(jù)庫(kù)

我想這部分就不用寫(xiě)了吧,你可以用任何方式(推薦使用存儲(chǔ)過(guò)程),將imageContent插入到數(shù)據(jù)庫(kù)中類型為image的字段就行了。

二、把圖片從數(shù)據(jù)庫(kù)中讀出

這部分比較簡(jiǎn)單:

假設(shè)img變量是你從數(shù)據(jù)庫(kù)中取出的圖片內(nèi)容
那么直接使用
Response.BinaryWrite(img)
就可以將圖片輸出到頁(yè)面上了

三:總結(jié)

將圖片存放在數(shù)據(jù)庫(kù)中其實(shí)是起到了圖片保護(hù)的作用,這樣就算別人瀏覽你的機(jī)器也看不到你的圖片,也可以用來(lái)保護(hù)重要的圖片資料。

相關(guān)文章

最新評(píng)論