پس تصمیم گرفتی عکس رو مستقیما بفرستی داخل DB.
* نام دیتابیس ما imgDB.mdf هست که با Access ایجاد شده، اگه با SQL ایجاد کردید فقط کافیه کانکشن رو تغییر بدین.
در Default.aspx فرم زیر رو درست کن:
* اولین Text-Box یک HTML File Input control هست.
جدول رو به شکل زیر ایجاد کن:
کد زیر رو برای کلید Browse در سورس صفحه قرار بده:
1 Protected Sub btnUpload_Click(..., ...) Handles btnUpload.Click
2
3 Dim intLength As Integer
4
5 Dim arrContent As Byte()
6
7
8
9 If fileUpload.PostedFile Is Nothing Then
10
11 lblStatus.Text = "No file specified."
12
13 Exit Sub
14
15 Else
16
17 Dim fileName As String = fileUpload.PostedFile.FileName
18
19 Dim ext As String = fileName.Substring(fileName.LastIndexOf("."))
20
21 ext = ext.ToLower
22
23
24
25 Dim imgType = fileUpload.PostedFile.ContentType
26
27 If ext = ".jpg" Then
28
29 ElseIf ext = ".bmp" Then
30
31 ElseIf ext = ".gif" Then
32
33 ElseIf ext = "jpg" Then
34
35 ElseIf ext = "bmp" Then
36
37 ElseIf ext = "gif" Then
38
39 Else
40
41 lblStatus.Text = "Only gif, bmp, or jpg format files supported."
42
43 Exit Sub
44
45 End If
46
47
48
49 intLength = Convert.ToInt32(fileUpload.PostedFile.InputStream.Length)
50
51 ReDim arrContent(intLength)
52
53
54
55 fileUpload.PostedFile.InputStream.Read(arrContent, 0, intLength)
56
57
58
59 If Doc2SQLServer(txtTitle.Text.Trim, arrContent, intLength, imgType) = True Then
60
61 lblStatus.Text = "Image uploaded successfully."
62
63 Else
64
65 lblStatus.Text = "An error occured while uploading Image... Please try again."
66
67 End If
68
69 End If
70
71 End Sub
72
برای ارتباط با DB:
1 Protected Function Doc2SQLServer(ByVal title As String, ByVal Content As Byte(), ByVal Length As Integer, ByVal strType As String) As Boolean
2
3 Try
4
5 Dim cnn As Data.SqlClient.SqlConnection
6
7 Dim cmd As Data.SqlClient.SqlCommand
8
9 Dim param As Data.SqlClient.SqlParameter
10
11 Dim strSQL As String
12
13
14
15 strSQL = "Insert Into tblImage(imgData,imgTitle,imgType,imgLength) Values(@content,@title,@type,@length)"
16
17
18
19 Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\imgDB.mdf"";Integrated Security=True;User Instance=True"
20
21 cnn = New Data.SqlClient.SqlConnection(connString)
22
23
24
25 cmd = New Data.SqlClient.SqlCommand(strSQL, cnn)
26
27
28
29 param = New Data.SqlClient.SqlParameter("@content", Data.SqlDbType.Image)
30
31
32
33 param.Value = Content
34
35 cmd.Parameters.Add(param)
36
37
38
39 param = New Data.SqlClient.SqlParameter("@title", Data.SqlDbType.VarChar)
40
41 param.Value = title
42
43 cmd.Parameters.Add(param)
44
45
46
47 param = New Data.SqlClient.SqlParameter("@type", Data.SqlDbType.VarChar)
48
49 param.Value = strType
50
51 cmd.Parameters.Add(param)
52
53
54
55 param = New Data.SqlClient.SqlParameter("@length", Data.SqlDbType.BigInt)
56
57 param.Value = Length
58
59 cmd.Parameters.Add(param)
60
61
62
63 cnn.Open()
64
65 cmd.ExecuteNonQuery()
66
67 cnn.Close()
68
69 Return True
70
71 Catch ex As Exception
72
73 Return False
74
75 End Try
76
77 End Function
78
یک صفحه ی جدید به نام imgGrab.aspx ایجاد کن و کد زیر رو بهش اضافه کن:
1 Protected Sub Page_Load(...,...) Handles Me.Load
2
3 Try
4
5 Dim ds As New DataSet
6
7 Dim da As SqlClient.SqlDataAdapter
8
9 Dim arrContent As Byte()
10
11 Dim dr As DataRow
12
13 Dim strSql As String
14
15
16
17 strSql = "Select * from tblImage Where imgId=" & Request.QueryString("ID")
18
19
20
21 Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\imgDB.mdf"";Integrated Security=True;User Instance=True"
22
23 da = New SqlClient.SqlDataAdapter(strSql, connString)
24
25 da.Fill(ds)
26
27 dr = ds.Tables(0).Rows(0)
28
29 arrContent = CType(dr.Item("imgData"), Byte())
30
31 Dim conType As String = dr.Item("imgType").ToString()
32
33 Response.ContentType = conType
34
35 Response.OutputStream.Write(arrContent, 0, dr.Item("imgLength"))
36
37 Response.End()
38
39 Catch ex As Exception
40
41
42
43 End Try
44
45 End Sub
46
حالا یک صفحه ی جدید به نام Viewer.aspx ایجاد کن، این صفحه باید توسط lnkView که در Default.aspx قرار دادیم لینک بشه. یک GridView بهش اضافه کن و اسمش رو به imgGrid تغییر بده.
روی EditColumns کلیک کن:
در این صفحه گزینه ی "Auto-generate fields" رو غیر فعال کن.
یک BoundField و یک ImageFiled اضافه کن و برای اولی caption رو title و DataField رو imgTitle انتخاب کن و برای دومی هم caption رو Picture و DataImageUrlField رو imgFile قرار بده.
کد زیر رو در pageLoad صفحه قرار بده:
1 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
2
3 Dim ds As New DataSet
4
5 Dim da As SqlClient.SqlDataAdapter
6
7 Dim strSQL As String
8
9
10
11 strSQL = "Select imgId,imgTitle from tblImage"
12
13 Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\imgDB.mdf"";Integrated Security=True;User Instance=True"
14
15 da = New SqlClient.SqlDataAdapter(strSQL, connString)
16
17 da.Fill(ds)
18
19
20
21 ds.Tables(0).Columns.Add("imgFile")
22
23
24
25 For Each tempRow As DataRow In ds.Tables(0).Rows
26
27 tempRow.Item("imgFile") = ("imgGrab.aspx?id=" & tempRow.Item("imgID"))
28
29 Next
30
31
32
33 imgGrid.DataSource = ds
34
35 imgGrid.DataBind()
36
37 End Sub
38
نتیجه ی نهایی به این صورت خواهد بود: