通用处理程序不返回值

本文关键字:返回值 程序 处理 | 更新日期: 2023-09-27 17:50:17

处理程序未返回图像。如果我删除条件语句,处理程序返回图像。这是我的代码

public void ProcessRequest(HttpContext context)
    {
        string sid = "JUN15MBACHN001";
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        connection.Open();
        SqlCommand command = new SqlCommand("select suppassportphoto from studdetails where sregno=" + sid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        Byte[] br = (Byte[])dr[0];
        if (br.Length > 1)
        {
            context.Response.BinaryWrite((Byte[])dr[0]);
        }          
        else
        {
            string path = context.Server.MapPath("~/image/emptymalepic.jpg");
            byte[] byteArray = File.ReadAllBytes(path);
            context.Response.BinaryWrite(byteArray);
        }
        connection.Close();
        context.Response.End();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

我不知道我哪里错了?

通用处理程序不返回值

首先,我建议以不同的方式进行强制转换,因为您的代码可能会导致无效的强制转换异常:

Byte[] br = dr[0] as Byte[];

然后检查是否为空

if (br != null && br.Length > 1)

然后写入文件:

context.Response.ContentType = "image/jpeg"; // or whatever type you're using
context.Response.BinaryWrite(br);

并替换

context.Response.End();

HttpContext.Current.ApplicationInstance.CompleteRequest();

因为我注意到有些浏览器不喜欢Response.End()

希望这是有用的。好运!

我可以注意到,在你的代码br。长度总是小于1

试试这个代码片段link

{
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
byte[] MyData= new byte[0];
da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];
MyData =  (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0); 
FileStream fs = new FileStream(@"C:'winnt'Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
}

try this

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim _ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString")
        Dim UserId As String = context.Request.QueryString("Id")
        Dim con As New SqlConnection(_ConnectionString)
        Try
            con.Open()
            Dim cmd As New SqlCommand(Convert.ToString("select UserPhoto from tblEmployee where EmpId=") & UserId, con)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            dr.Read()
            If Not dr.IsDBNull(0) Then
                context.Response.BinaryWrite(DirectCast(dr(0), Byte()))
            Else
                Dim imgpath As String = context.Server.MapPath("~/images/images.jpg")
                Dim byteArray As Byte() = File.ReadAllBytes(imgpath)
                context.Response.BinaryWrite(byteArray)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub