首页 > 解决方案 > 从 Live Sql 表 ASP.NET 中看不到二进制图像

问题描述

在我的页面上,我必须上传图像并在保存后立即显示在ImageButton控制和Image控制上。它在本地工作,但不在Stagging Server. 我已经尝试过ImageDatatype 和VARBINARY(MAX). 在这两种情况下,图像都没有显示在stagging server.

本地截图 在此处输入图像描述

暂存服务器截图 在此处输入图像描述

下面是我的代码:

网页

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Submit" /><br /><br />
        <asp:Image ID="Image1" runat="server" Width="150px" />
        <asp:ImageButton ID="ImageButton1" runat="server" Width="150px"/>
    </form>
</body>
</html>

VB代码

Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Partial Class testingImage
    Inherits System.Web.UI.Page
    Dim imageurl As String = ""
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        bindrepeater()
    End Sub

    Sub bindrepeater()
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("dsejConnectionString").ConnectionString)
            Using cmdda As New SqlDataAdapter("select imagefile from testingPhototable where id=1", conn)
                Using ds As New DataSet()
                    cmdda.Fill(ds, "t")
                    If (ds.Tables(0).Rows.Count > 0) Then
                        If ds.Tables(0).Rows(0)("imagefile").ToString() <> "" Then
                            imageurl = "data:image/jpg;base64," & Convert.ToBase64String(CType(ds.Tables(0).Rows(0)("imagefile"), Byte()))
                            ImageButton1.ImageUrl = imageurl
                            Image1.ImageUrl = imageurl
                        End If
                    End If
                End Using
            End Using
        End Using
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim bytes As Byte()
        Using br As BinaryReader = New BinaryReader(FileUpload1.PostedFile.InputStream)
            bytes = br.ReadBytes(FileUpload1.PostedFile.ContentLength)
        End Using

        Dim constr As String = ConfigurationManager.ConnectionStrings("dsejConnectionString").ConnectionString
        Using conn As SqlConnection = New SqlConnection(constr)
            Dim sql As String = "UPDATE testingPhototable set imagefile=@imagefile where id=1"
            Using cmd As SqlCommand = New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@imagefile", bytes)
                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
            End Using
        End Using
        bindrepeater()
    End Sub
End Class

标签: asp.netvb.net

解决方案



推荐阅读