views:

121

answers:

2

does anyone know how to upload an image from asp.net using vb.net i need a browse and upload button on aspx side. i already have an image field in the table in sql server 2008. thanks for helping

A: 

1.Add a fileupload control 2. Add an upload button, and place this in the button click event handler:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim bldgIDNum As Int32 = FormView_Building.SelectedValue
    If FileUpload1.PostedFile IsNot Nothing AndAlso FileUpload1.PostedFile.FileName <> "" Then
        Dim imageSize As Byte() = New Byte(FileUpload1.PostedFile.ContentLength - 1) {}
        Dim uploadedImage__1 As HttpPostedFile = FileUpload1.PostedFile
        uploadedImage__1.InputStream.Read(imageSize, 0, CInt(FileUpload1.PostedFile.ContentLength))

        ' Create SQL Connection 
        Dim con As New SqlConnection()
        con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

        ' Create SQL Command 

        Dim cmd As New SqlCommand()
        cmd.CommandText = "INSERT INTO Table (PrimaryKey,ImageData) VALUES (@PrimaryKey,@ImageData)"
        cmd.CommandType = CommandType.Text
        cmd.Connection = con

        Dim PrimaryKey As New SqlParameter("@PrimaryKey", SqlDbType.Int, 32)
        PrimaryKey.Value = (however you want to get it)

        Dim UploadedImage__2 As New SqlParameter("@ImageData", SqlDbType.Image, imageSize.Length)
        UploadedImage__2.Value = imageSize
        cmd.Parameters.Add(UploadedImage__2)
        con.Open()
        Dim result As Integer = cmd.ExecuteNonQuery()
        con.Close()
        If result > 0 Then
            lblMessage.Text = "File Uploaded"
        End If

    End If

    ListView_BldgImages.DataBind()

End Sub
  1. the database column ImageData should be varbinary(max)

  2. Create a handler called Handler_Image.ashx with the following content:

Imports System Imports System.Web Imports System.Configuration Imports System.Data.SqlClient

Public Class Handler_Image : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim con As New SqlConnection()
    con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

    ' Create SQL Command 

    Dim cmd As New SqlCommand()
    cmd.CommandText = "Select ImageData from Table where PrimaryKey =@PrimaryKey"
    cmd.CommandType = System.Data.CommandType.Text
    cmd.Connection = con

    Dim ID As New SqlParameter("@PrimaryKey", System.Data.SqlDbType.Int)
    ID.Value = context.Request.QueryString("PrimaryKey")
    cmd.Parameters.Add(ID)
    con.Open()
    Dim dReader As SqlDataReader = cmd.ExecuteReader()
    dReader.Read()
    context.Response.BinaryWrite(DirectCast(dReader("ImageData"), Byte()))
    dReader.Close()
    con.Close()
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

End Class

  1. display the image with an image control with the following property: ImageUrl='<%# "Handler_Image.ashx?PrimaryKey=" & Eval("PrimaryKey")%>'

6.replace the connectionstring, the table name, and the primary key to suit your application

jason