本文介绍了如何检查是否图像在图像控件中存在一个处理程序来显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
处理程序
公共类Handler2:IHttpHandler的
{
公共无效的ProcessRequest(HttpContext的背景下)
{
字符串ID = context.Request.QueryString [ID];
字符串构造= System.Configuration.ConfigurationManager
.ConnectionStrings [EmployeeDatabase]的ConnectionString。
SqlConnection的CON =新的SqlConnection(构造); con.Open();
CMD的SqlCommand =新的SqlCommand(的getImageCON);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@的EmpID,身份证);
//cmd.Parameters.AddWithValue(\"@Gender,GE);
SqlDataReader的博士= cmd.ExecuteReader();
dr.Read(); 如果(dr.HasRows)
{
字节[] = imageBytes(字节[])dr.GetValue(0);
context.Response.BinaryWrite(imageBytes);
} dr.Close();
con.Close();
}
用户控制
公共无效BindGridviewData()
{
字符串empid1 =名称;
//字符串empid1 =的Request.QueryString [MYTEXT];
INT EMPID = int.Parse(empid1);
// INT EMPID = 1500422;
字符串构造= System.Configuration.ConfigurationManager
.ConnectionStrings [EmployeeDatabase]的ConnectionString。
SqlConnection的CON =新的SqlConnection(构造); con.Open();
CMD的SqlCommand =新的SqlCommand(GetEmployeeDetailsCON);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@的EmpID,SqlDbType.Int,0).value的= EMPID;
SqlDataReader的博士= cmd.ExecuteReader();
dr.Read(); Label9.Text =博士[0]的ToString();
Label2.Text =博士[1]的ToString();
Label3.Text =博士[2]的ToString();
Label4.Text =博士[3]的ToString();
Label5.Text =博士[4]的ToString();
Label6.Text =博士[5]的ToString();
Label7.Text =博士[6]的ToString();
Label8.Text =博士[7]的ToString(); Image2.ImageUrl =〜/ Handler2.ashx n =?+ EMPID;
}
在上面的程序,如果我们没有在图像控制越来越像,我们需要显示在按钮上的文字为添加图片,如果我们有像我们需要显示其为更新映像
我用下面这些不工作的方式:
选项1
如果(Image2.ImageUrl!= NULL)
{
PageLinkButton.Text =UPADTE你的影像;
}
其他
{
PageLinkButton.Text =添加映像;
}
选项2
Web客户端的客户端=新的WebClient();
字节[]值= client.DownloadData(Image2.ImageUrl);如果(值!= NULL)
{
PageLinkButton.Text =更新形象;
}
其他
{
PageLinkButton.Text =添加映像;
}
解决方案
您可以有你的通用处理程序返回404状态code如果图像不存在:
公共无效的ProcessRequest(HttpContext的背景下)
{
字符串ID = context.Request.QueryString [ID];
字符串构造= System.Configuration.ConfigurationManager.ConnectionStrings [EmployeeDatabase]的ConnectionString。
使用(VAR CON =新的SqlConnection(构造))
使用(VAR CMD = con.CreateCommand())
{
con.Open();
cmd.CommandText =的getImage;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@的EmpID,身份证);
使用(VAR博士= cmd.ExecuteReader())
{
如果(dr.Read())
{
//图像被发现
字节[] = imageBytes(字节[])dr.GetValue(0);
//不要忘记设置正确的内容类型
context.Response.ContentType =图像/ PNG
context.Response.BinaryWrite(imageBytes);
}
其他
{
//没有在数据库= GT找到的记录;返回404
context.Response.Status code = 404;
}
}
}
}
和然后在客户端上可以使用一个Web客户端来测试,如果图像中存在
VAR图片网址=〜/ Handler2.ashx n =?+ EMPID;
VAR基本URI =新的URI(Request.Url.GetLeftPart(UriPartial.Authority));
VAR URL =新的URI(基本URI,VirtualPathUtility.ToAbsolute(图片网址));
使用(VAR的客户=新的HttpClient())
{
VAR要求=新的Htt prequestMessage(HttpMethod.Head,URL);
VAR响应= client.SendAsync(要求)。结果;
如果(response.IsSuccessStatus code)
{
//图像存在:
PageLinkButton.Text =更新形象;
Image2.ImageUrl =图片网址;
}
其他
{
//图像不存在:
PageLinkButton.Text =添加映像;
}
}
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();
}
User Control
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;
}
In the above program if we are not getting image in image control we need to display the text on button as "Add your Image" and if we have image we need to display it as "Update your Image"
I have used following ways which are not working:
Option 1
if (Image2.ImageUrl != null)
{
PageLinkButton.Text = "Upadte your Image";
}
else
{
PageLinkButton.Text = "Add your Image";
}
Option 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";
}
解决方案
You could have your generic handler return 404 status code if the image doesn't exist:
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;
}
}
}
}
and then on the client you could use a WebClient to test if the image exists:
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";
}
}
这篇关于如何检查是否图像在图像控件中存在一个处理程序来显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!