如何使用用于显示图像的处理程序检查图像控件中是否存在图像

本文关键字:图像 检查 控件 存在 是否 程序 用于 何使用 显示 显示图 处理 | 更新日期: 2023-09-27 17:56:53

Handler

public class Handler2 : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string id = context.Request.QueryString["Id"];
        string constr = System.Configuration.ConfigurationManager
            .ConnectionStrings["EmployeeDatabase"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlCommand cmd = new SqlCommand("GetImage", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EmpID", id);
        //cmd.Parameters.AddWithValue("@Gender", ge);
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        if (dr.HasRows)
        {
            byte[] imageBytes = (byte[])dr.GetValue(0);
            context.Response.BinaryWrite(imageBytes);
        }
        dr.Close();
        con.Close();
    }

用户控制

public void BindGridviewData()
{
    String empid1 = Name;
    // String empid1 = Request.QueryString["MyText"];
    int empid = int.Parse(empid1);
    //int empid = 1500422;
    string constr = System.Configuration.ConfigurationManager
        .ConnectionStrings["EmployeeDatabase"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand cmd = new SqlCommand("GetEmployeeDetails", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@EmpID", SqlDbType.Int, 0).Value = empid;
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    Label9.Text = dr[0].ToString();
    Label2.Text = dr[1].ToString();
    Label3.Text = dr[2].ToString();
    Label4.Text = dr[3].ToString();
    Label5.Text = dr[4].ToString();
    Label6.Text = dr[5].ToString();
    Label7.Text = dr[6].ToString();
    Label8.Text = dr[7].ToString();
    Image2.ImageUrl = "~/Handler2.ashx?Id=" + empid;
}

在上面的程序中,如果我们在图像控件中没有获取图像,我们需要将按钮上的文本显示为"添加您的图像",如果我们有图像,我们需要将其显示为"更新您的图像"

我使用了以下不起作用的方法:

选项 1

 if (Image2.ImageUrl != null)
 {
     PageLinkButton.Text = "Upadte your Image";
 }
 else
 {
     PageLinkButton.Text = "Add your Image";
 } 

选项 2

WebClient client = new WebClient();
byte[] Value = client.DownloadData(Image2.ImageUrl);
if (Value != null)
{
    PageLinkButton.Text = "Update your Image";
}
else
{
    PageLinkButton.Text = "Add your Image";
}  

如何使用用于显示图像的处理程序检查图像控件中是否存在图像

如果图像不存在,则可以让泛型处理程序返回 404 状态代码:

public void ProcessRequest(HttpContext context)
{
    string id = context.Request.QueryString["Id"];
    string constr = System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeDatabase"].ConnectionString;
    using (var con = new SqlConnection(constr))
    using (var cmd = con.CreateCommand())
    {
        con.Open();
        cmd.CommandText = "GetImage";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EmpID", id);
        using (var dr = cmd.ExecuteReader())
        {
            if (dr.Read())
            {
                // the image was found
                byte[] imageBytes = (byte[])dr.GetValue(0);
                // Don't forget to set the proper content type
                context.Response.ContentType = "image/png";
                context.Response.BinaryWrite(imageBytes);
            }
            else
            {
                // no record found in the database => return 404
                context.Response.StatusCode = 404;
            }
        }
    }
}

然后在客户端上,您可以使用 Web 客户端来测试图像是否存在:

var imageUrl = "~/Handler2.ashx?Id=" + empid;
var baseUri = new Uri(Request.Url.GetLeftPart(UriPartial.Authority));
var url = new Uri(baseUri, VirtualPathUtility.ToAbsolute(imageUrl));
using (var client = new HttpClient())
{
    var request = new HttpRequestMessage(HttpMethod.Head, url);
    var response = client.SendAsync(request).Result;
    if (response.IsSuccessStatusCode)
    {
        // the image exists:
        PageLinkButton.Text = "Update your Image";
        Image2.ImageUrl = imageUrl;
    }
    else
    {
        // the image does not exist:
        PageLinkButton.Text = "Add your Image";
    }
}