【问题标题】:Binary Image Not Visible From Live Sql Table ASP.NET从 Live Sql 表 ASP.NET 中看不到二进制图像
【发布时间】:2021-07-11 08:17:48
【问题描述】:

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

本地截图

暂存服务器屏幕截图

下面是我的代码:

HTML

<!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

【问题讨论】:

  • 检查你的临时服务器上img标签的src属性,你在那里得到了什么值?
  • 图片在SRC中的二进制路径值。

标签: asp.net vb.net


【解决方案1】:

在我的 web.config

<httpProtocol>
      <customHeaders>
        <add name="X-Frame-Options" value="DENY"/>
        <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
        <add name="X-XSS-Protection" value="1; mode=block" />
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="content-security-policy: frame-ancestors 'none';img-src 'self';object-src 'self';frame-src 'self';child-src 'self';base-uri 'self';"/>
        <add name="Referrer-Policy" value="strict-origin" />
      </customHeaders>
    </httpProtocol>

正如上面content-security-policy 下列出的,有一个属性img-src 'self'; 这将停止二进制图像以控制渲染。我已经删除了它,现在它工作正常。

【讨论】:

    【解决方案2】:

    由于属性名称ImageUrl 是由 “Url” 之类的词组成的,因此假定需要一个 URL。因此,它不像 客户端 上的“src”属性,您也可以将源设置为 Base64 字符串。 ImageUrl 需要一个 relative url,例如“root/images/image1.jpg”或absolute“//somewhere_in_the_net_or_server/anotherpath/specificpath/yourfile.jpg”。无论如何,要实现您想要的,您可以设置 ImageButton1 的属性“src”,如下代码行所示:

    ImageButton1.Attributes("src") = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAAA3NCSVQICAjb4U....................."
    

    Image1 也可以这样做

    【讨论】:

    • 它在本地运行良好,但在暂存服务器上仍然无法运行。 Staging Server 是否有任何问题会阻止图像显示?或者可能没有正确发布?
    • 确保您已通过 IIS 为您尝试设置的扩展名(.jpg、.png 或 .svg 等)启用了 mime 类型,然后如果没有任何反应,当您执行正在通过数据库或在字节转换期间获取您的 img。
    • 我要说的另一件事是:为什么您要调用 DataSet 使事情变得如此繁重,以至于您可以使用 SqlDataReader 轻松完成?
    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2011-11-15
    相关资源
    最近更新 更多