【发布时间】: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中的二进制路径值。