无法通过图像处理程序检索图像控制中的图像

时间:2013-04-28 13:57:58

标签: c# asp.net image handler

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

我的数据库ImageDatabase包含一个表Pict,其中包含两列Lettervarchar(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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

在BinaryWrite之前尝试清除响应流

context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(bytes);
context.Response.End();