這是我的初級程序員extraodinaireasp.net存儲圖像
我想從我的數據庫顯示圖像,但是當我嘗試提取它僅包含6個字節的數據的文件,該文件會導致我的程序拋出一個錯誤。我的導師告訴我,該文件對於圖像太小,導致我相信我沒有正確存儲它。如果有人能夠識別可能導致這種情況發生的任何代碼,我會非常高興。
這是我的功能斂/存儲文件
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim _image As Byte()
_image = FileUpload1.FileBytes
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
cmd.Connection = conn
cmd.CommandText = "Insert INTO mrg_Image(Image, UserId) VALUES('@image', @id)"
cmd.Parameters.AddWithValue("@image", FileUpload1.FileBytes)
cmd.Parameters.AddWithValue("@id", 4)
conn.Open()
dr = cmd.ExecuteReader
conn.Close()
'FileUpload1.SaveAs()
End Sub
conn.Open()
file_bytes是包含6個字節作爲反對到一千+其中圖像應當具有
Dim file_bytes As Byte() = cmd.ExecuteScalar()
Dim file_bytes_memory_stream As New System.IO.MemoryStream(file_bytes)
Dim file_bytes_stream As System.IO.Stream = DirectCast(file_bytes_memory_stream, System.IO.Stream)
Dim the_image As New System.Drawing.Bitmap(file_bytes_stream)
context.Response.ContentType = "image/jpg"
the_image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
conn.Close()
End Sub
HasFile是真的嗎?將FileBytes數組保存到數據庫之前,它的大小是多少? – Will 2010-09-23 18:04:25
什麼類型的字段是'圖片'字段? – CodingGorilla 2010-09-23 18:04:34
什麼是數據類型的字段圖像,以及爲什麼圖像參數值的單引號? – 2010-09-23 18:05:39