asp.net - 从 Live Sql 表 ASP.NET 中看不到二进制图像
问题描述
在我的页面上,我必须上传图像并在保存后立即显示在ImageButton
控制和Image
控制上。它在本地工作,但不在Stagging Server
. 我已经尝试过Image
Datatype 和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
解决方案
推荐阅读
- javascript - 当没有使用 JS / jQuery 的返回 URL 时,如何隐藏我的站点中的返回按钮?
- android - Android Studio logcat 打印很多错误
- php - 密码有效 12 小时
- azure - 使用 azure api 获得最高支出资源
- ruby - `initialize':参数数量错误(给定 0,预期 2)(ArgumentError)
- r - 改变语言学习教程
- html - HTML 范围输入最小值未按预期工作
- delphi - 通用树节点结构:释放子节点的问题
- c# - 如何在给定时间后取消异步任务以及如何重新启动失败的任务?
- amazon-cloudformation - 如何让 Amplify CloudFormation 了解在其外部所做的更改