0

我成功地将图像存储在数据库中,但我无法检索它......

我有ImageDatabase一个Pict包含两列Lettervarchar(50))(pk)和Pictureimage)的表的数据库,

我的Handler.ashx代码:

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;
    }
}

}

我的默认.aspx

<img id="img1" src="Handler.ashx?Letter='a'" />

请在这方面帮助我,我犯了错误,

4

1 回答 1

0

在 BinaryWrite 之前尝试清除响应流

context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(bytes);
context.Response.End();
于 2013-04-28T14:50:04.140 回答