预览图像功能导致文件上传控件重置



我的图片上传的代码片段:

'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



处理程序1.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. 预览 - 将发布的文件保存在会话中。将 Image1 图像 URL 设置为处理程序。
  2. 处理程序 - 从会话获取已发布的文件。将图像写入响应。
  3. 上传 - 检查文件上传 1 中是否存在文件,获取它。如果没有,请获取它从会话。保存图像。清除会话。

这是我将使用的代码:

编辑:更改了代码以修复较大(> 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

处理程序1.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

希望对您有所帮助!

最新更新