从Live Sql表ASP.NET中看不到二进制图像
在我的页面上,我必须上传图像,保存后立即显示在从Live Sql表ASP.NET中看不到二进制图像,asp.net,vb.net,Asp.net,Vb.net,在我的页面上,我必须上传图像,保存后立即显示在ImageButton控件和image控件上。它在本地工作,但不在分段服务器上运行。 我尝试了ImageDatatype和VARBINARY(MAX)。在这两种情况下,图像都不会显示在分段服务器上 本地屏幕截图 暂停服务器屏幕截图 下面是我的代码: Html: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head ru
ImageButton
控件和image
控件上。它在本地工作,但不在分段服务器上运行。
我尝试了Image
Datatype和VARBINARY(MAX)
。在这两种情况下,图像都不会显示在分段服务器上
本地屏幕截图
暂停服务器屏幕截图
下面是我的代码:
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>
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
vb代码:
<!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>
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
Imports System.IO
导入系统数据
导入系统配置
导入System.Data.SqlClient
部分类测试图像
继承System.Web.UI.Page
Dim imageurl As String=“”
受保护的子页加载(发件人作为对象,e作为事件参数)处理Me.Load
bindrepeater()
端接头
子转发器()
将conn用作新的SqlConnection(ConfigurationManager.ConnectionString(“dsejConnectionString”).ConnectionString)
将cmdda用作新的SqlDataAdapter(“从testingPhototable中选择imagefile,其中id=1”,conn)
使用ds作为新数据集()
cmdda.Fill(ds,“t”)
如果(ds.Tables(0).Rows.Count>0),则
如果ds.Tables(0).Rows(0)(“imagefile”).ToString()”,则
imageurl=“data:image/jpg;base64,”&Convert.ToBase64String(CType(ds.Tables(0).行(0)(“imagefile”),Byte())
ImageButton1.ImageUrl=ImageUrl
Image1.ImageUrl=ImageUrl
如果结束
如果结束
终端使用
终端使用
终端使用
端接头
受保护的子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
作为字节()的Dim字节
使用br As BinaryReader=New BinaryReader(FileUpload1.PostedFile.InputStream)
bytes=br.ReadBytes(FileUpload1.PostedFile.ContentLength)
终端使用
Dim constr As String=ConfigurationManager.ConnectionString(“dsejConnectionString”).ConnectionString
使用conn作为SqlConnection=新的SqlConnection(cont)
Dim sql As String=“UPDATE testingPhototable set imagefile=@imagefile,其中id=1”
使用cmd作为SqlCommand=newsqlcommand(sql,conn)
cmd.Parameters.AddWithValue(“@imagefile”,字节)
康涅狄格州公开赛
cmd.ExecuteNonQuery()
康涅狄格州关闭
终端使用
终端使用
bindrepeater()
端接头
末级
由于属性名ImageUrl
是由“Url”这样的词构成的,因此假定需要Url。因此,它不像客户端上的“src
”属性,您可以将源代码设置为Base64字符串。
ImageUrl
需要一个相对的url,如“root/images/image1.jpg”或绝对的“//在网络或服务器的某处/anotherpath/specificpath/yourfile.jpg”。无论如何,要实现您想要的,您可以设置ImageButton1的属性“src”,如下代码行所示:
ImageButton1.Attributes("src") = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAAA3NCSVQICAjb4U....................."
您也可以在myweb.config中对
Image1
执行相同的操作
<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>
如上所述,在
内容安全策略下有一个属性img src'self'代码>这将停止二进制图像以获取控件上的渲染。我已经删除了这个,现在它工作正常。检查你的暂存服务器上的img
标记的src
属性,你在那里得到了什么值?它是src中图像的二进制路径值。它在本地服务器上工作正常,但在暂存服务器上仍然不工作。暂停服务器是否会导致图像停止显示?或者可能未正确发布?请确保IIS已为您试图设置的扩展名(.jpg、.png或.svg等)启用mime类型然后,如果什么也没有发生,那么当您通过数据库获取img或在转换过程中以字节为单位获取img时,似乎出现了一些问题。我必须说的另一件事是:为什么您要调用一个数据集,使事情变得如此繁重,以便用SqlDataReader轻松完成一些事情?