Is there any way an image can be uploaded with ASP.NET with VB?
I need an "upload" button on the ASP.NET side.
I already have an image field in the table in SQL server 200开发者_开发问答8.
Is there any way I can do this?
- Add a 
fileuploadcontrol with: 
<asp:FileUpload runat="server" id="FileUpload1" /> 
- 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
The database column
ImageDatashould bevarbinary(max)Create a handler called
Handler_Image.ashxwith 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
Display the image with an image control with the following property:
ImageUrl='<%# "Handler_Image.ashx?PrimaryKey=" & Eval("PrimaryKey")%>'Replace the
connectionstring, the table name, and the primary key to suit your application
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
 加载中,请稍侯......
      
精彩评论