开发者

asp.net storing images

开发者 https://www.devze.com 2023-01-17 03:40 出处:网络
It\'s me your junior programmer extraodinaire I\'m trying to display an image from my database but when I try to extract the file it only contains 6 bytes of data which causes my program to throw an

It's me your junior programmer extraodinaire

I'm trying to display an image from my database but when I try to extract the file it only contains 6 bytes of data which causes my program to throw an error. My instructor informed me that the file is too small for an image, leading me to believe that I'm not storing it properly. I would be most gracious if someone could identify any code that might cause this to happen.

this is my function grabbing/storing the file

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 Sq开发者_C百科lCommand

    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 is the variable that contains 6 bytes as oppose to a thousand+ which an image should have

    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


Try replacing '@image' with @image.

Also replace ExecuteReader with ExecuteNonQuery.


I wasn't aware of the FileBytes property, I've always done it so...

Dim _image(FileUpload1.PostedFile.InputStream.Length) As Byte FileUpload1.PostedFile.InputStream.Read(_image, 0, _image.Length)


Try this

cmd.CommandText = "Insert INTO mrg_Image(Image, UserId) VALUES(@image, @id)"
cmd.Parameters.Add("@image", SqlDbType.Image)
cmd.Parameters("@image") = FileUpload1.FileBytes
cmd.Parameters.AddWithValue("@id", 4)

Edit Removed ";" forgot this was vb

Edit2 Changed [] to (). again forgot my VB

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号