我已成功将图像存储在数据库中,但我无法检索它......
我的数据库ImageDatabase
包含一个表Pict
,其中包含两列Letter
(varchar(50)
)(pk)和Picture
({{1} }),
我的image
代码:
Handler.ashx
}
我的default.aspx
using System;
using System.Web;
using System.Data.SqlClient;
using System.IO;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string L = (context.Request.QueryString["Letter"]);
SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"Data Source=RANJHANI-PC\SQLEXPRESS;Initial Catalog=ImageDatabase;Integrated Security=True";
string sql = "SELECT Picture FROM Pict WHERE Letter = @Ltr";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@Ltr", L);
connection.Open();
byte[] bytes = (byte[]) cmd.ExecuteScalar();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(bytes);
connection.Close();
}
public bool IsReusable {
get {
return false;
}
}
请在这方面帮助我,我犯了错误,
答案 0 :(得分:0)
在BinaryWrite之前尝试清除响应流
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(bytes);
context.Response.End();