![](/img/trans.png)
[英]ASP.NET C# Get retrieve and show image from SQL Server database
[英]How to retrieve image fields civilimage1,civilimage2 from SQL Server database to show using image control in ASP.NET with vb.net or c#?
我在SQL Server数据库字节数组中有一些图像,我想在ASP.NET中检索这些图像。 但我没有得到images.Picture写在文件夹内,但我不能在图像控制器中播种它们。
请解决我的问题。
Private Sub CusImgShow1()
{
Const SQL As String = "SELECT civilimg1 , civilimg2 FROM [NewCus] WHERE [Cust_ID] = @Cust_ID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@Cust_ID", Convert.ToInt32(Request.QueryString("Cust_ID")))
myConnection.Open()
Dim dr As SqlDataReader = myCommand.ExecuteReader()
Try
If dr.Read() Then
Dim image As String = Convert.ToString(DateTime.Now.ToFileTime()) & "1"
Dim image3 As String = Convert.ToString(DateTime.Now.ToFileTime()) & "2"
Dim fs1 As New FileStream(("~/CRM/imagesb/" & image), FileMode.Create, FileAccess.Write)
Dim fs2 As New FileStream(("~/CRM/imagesb/" & image3), FileMode.Create, FileAccess.Write)
Dim bimage1 As Byte() = DirectCast(dr("civilimg1"), Byte())
Dim bimage2 As Byte() = DirectCast(dr("civilimg2"), Byte())
fs1.Write(bimage1, 0, bimage1.Length - 1)
fs2.Write(bimage2, 0, bimage2.Length - 1)
fs1.Flush()
fs2.Flush()
Image1.ImageUrl = "~/CRM/imagesb/" & image
Image2.ImageUrl = "~/CRM/imagesb/" & image3
'Dim base64String As String = Convert.ToBase64String(bimage1, 0, bimage1.Length)
'Image1.ImageUrl = Convert.ToString("image/JPEG") & base64String
End If
myConnection.Close()
Catch ex As Exception
Throw ex
End Try
}
1)创建一个句柄(.ashx)文件,该文件从查询字符串获取图像名称并将图像发送到浏览器
public class Myandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
byte[] fileBytes;
string fileName;
//...get from byte[] from SQL Server
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Length", fileBytes.Length.ToString());
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.BinaryWrite(fileBytes);
context.Response.Flush();
}
}
2)进入HTML页面
<img src="MyHandler.ashx?ImageId=123">
注意:对不起,我的代码是c#....
注意2:这只是一个示例答案..您可以加密查询字符串,验证用户等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.