预览图像功能导致FileUpload控件重置

时间:2013-12-21 19:11:44

标签: asp.net vb.net image file-upload preview

我的图片上传的代码段:

'Read the file and convert it to Byte Array
 Dim filePath As String = FileUpload1.PostedFile.FileName
 Dim filename As String = Path.GetFileName(filePath)
 Dim ext As String = Path.GetExtension(filename)
 Dim contenttype As String = String.Empty

 'Set the contenttype based on File Extension
 Select Case ext
      Case ".jpg"
           contenttype = "image/jpg"
           Exit Select
      Case ".png"
           contenttype = "image/png"
           Exit Select
 End Select

If Not contenttype Is String.Empty Then

 Dim fs As Stream = FileUpload1.PostedFile.InputStream
 Dim br As New BinaryReader(fs)
 Dim bytes As Byte() = br.ReadBytes(fs.Length)

 'insert the file into database
 cmd.Parameters.Add("@imgName", SqlDbType.VarChar).Value = filename
 cmd.Parameters.Add("@contentType", SqlDbType.VarChar).Value = contenttype
 cmd.Parameters.Add("@data", SqlDbType.Binary).Value = bytes

Else

 cmd.Parameters.Add("@imgName", SqlDbType.VarChar).Value = DBNull.Value
 cmd.Parameters.Add("@contentType", SqlDbType.VarChar).Value = DBNull.Value
 cmd.Parameters.Add("@data", SqlDbType.Binary).Value = DBNull.Value

End If

con.Open()
cmd.ExecuteNonQuery()
con.Close()



预览图片:

Protected Sub preview_btn_Click(sender As Object, e As EventArgs) Handles preview_btn.Click
    Session("ImageBytes") = FileUpload1.FileBytes
    Image1.ImageUrl = "~/Handler1.ashx"

    preview_btn.BackColor = Drawing.Color.Lime
    preview_btn.ForeColor = Drawing.Color.White

    Image1.BorderColor = Drawing.Color.Lime
End Sub



Handler1.ashx

<%@ WebHandler Language="VB" Class="Handler1" %>

Imports System
Imports System.Web

Public Class Handler1 : Implements System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    If (context.Session("ImageBytes")) IsNot Nothing Then
        Dim image As Byte() = DirectCast(context.Session("ImageBytes"), Byte())
        context.Response.ContentType = "image/jpeg"
        context.Response.BinaryWrite(image)
    End If
End Sub

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

End Class



预览图像有效但每当我调用预览图像按钮时,FileUpload控件都会重置。

所以从FileUpload控件的角度来看,就好像用户没有首先选择任何图像一样。

我尝试先将FileUpload1.PostedFile.FileName的值存储在某个变量中,然后手动设置其值,但FileUpload1.PostedFile.FileName似乎是只读的,从安全角度来看这是有道理的。

为此解决这个问题?

1 个答案:

答案 0 :(得分:1)

要快速解决方案,我会执行以下操作:

  1. 预览 - 将发布的文件保存在会话中。将Image1图像URL设置为处理程序。
  2. 处理程序 - 从会话中获取发布的文件。将图像写入回复。
  3. 上传 - 检查FileUpload1中是否存在文件,获取它。如果没有,那就去吧 来自会议。保存图片。清除会话。
  4. 以下是我将使用的代码:

    编辑:更改代码以修复较大(> 50kb)图片的问题

    <强> aspx.vb

    Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
        'Disabled DB operations for test
        'Read the file and convert it to Byte Array
        Dim filePath As String = String.Empty
        Dim filename As String = String.Empty
        Dim ext As String = String.Empty
        Dim contenttype As String = String.Empty
        Dim bytes As Byte()
    
        If FileUpload1.HasFile Then
            filePath = FileUpload1.PostedFile.FileName
        Else
            If (Session("MyFile") IsNot Nothing AndAlso Session("MyFileName") IsNot Nothing) Then
                filePath = Session("MyFileName").ToString()
                bytes = DirectCast(Session("MyFile"), Byte())
            End If
        End If
    
        filename = Path.GetFileName(filePath)
        ext = Path.GetExtension(filename)
    
        'Set the contenttype based on File Extension
        Select Case ext
            Case ".jpg"
                contenttype = "image/jpg"
                Exit Select
            Case ".png"
                contenttype = "image/png"
                Exit Select
        End Select
    
        If Not contenttype Is String.Empty Then
            If FileUpload1.HasFile Then
                Dim fs As Stream = FileUpload1.PostedFile.InputStream
                Dim br As New BinaryReader(fs)
                bytes = br.ReadBytes(fs.Length)
            End If
            'insert the file into database
            cmd.Parameters.Add("@imgName", SqlDbType.VarChar).Value = filename
            cmd.Parameters.Add("@contentType", SqlDbType.VarChar).Value = contenttype
            cmd.Parameters.Add("@data", SqlDbType.Binary).Value = bytes
    
        Else
    
            cmd.Parameters.Add("@imgName", SqlDbType.VarChar).Value = DBNull.Value
            cmd.Parameters.Add("@contentType", SqlDbType.VarChar).Value = DBNull.Value
            cmd.Parameters.Add("@data", SqlDbType.Binary).Value = DBNull.Value
    
        End If
    
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    
        'Cleanup
        Session("MyFile") = Nothing
        Session("MyFileName") = Nothing
    End Sub
    
    Protected Sub preview_btn_Click(sender As Object, e As EventArgs) Handles preview_btn.Click
        If FileUpload1.PostedFile IsNot Nothing Then
            Dim file As HttpPostedFile = FileUpload1.PostedFile
    
            Dim data As Byte() = New [Byte](file.ContentLength - 1) {}
            file.InputStream.Read(data, 0, file.ContentLength)
    
            Session("MyFile") = data
            Session("MyFileName") = FileUpload1.PostedFile.FileName
            Image1.ImageUrl = "~/Handler1.ashx"
    
            preview_btn.BackColor = Drawing.Color.Lime
            preview_btn.ForeColor = Drawing.Color.White
    
            Image1.BorderColor = Drawing.Color.Lime
        End If
    End Sub
    

    <强> Handler1.ashx

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        If (context.Session("MyFile")) IsNot Nothing Then
            Dim storedImage = TryCast(context.Session("MyFile"), Byte())
            If storedImage IsNot Nothing Then
                context.Response.ContentType = "image/jpeg"
                context.Response.BinaryWrite(storedImage)
            End If
        End If
    End Sub
    

    希望它有所帮助!