I have read a lot about how to display binary images on the page. They are very good, but they do not think they are very practical. I want to introduce a method that is more practical.
Use the "General handler ". If you are using vs2005, you can see the "General handler" in adding a new project. Its suffix is. ashx. What is it? In fact, it is similar to. aspx. First, ask. How does aspx work? As you may know,. aspx can process external incoming requests, and then it can process this request and generate an HTML file to return the result. This is a typical way to process external requests .. Aspx is specially designed to handle "typical" requests. So what should we do if we need to process an external request and a custom request? (That is, the "typical" method is not used for processing ).. Ashx can help you do this.
How can I help you build what you want? This is what I want to talk about. But you still have to check what. ashx is.
<% @ Webhandler Language = "C #" class = "imagehandler" %>
Using system;
Using system. Web;
/// <Summary>
/// This is a general handler without any implementation.
/// </Summary>
Public class imagehandler: ihttphandler {
Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
Context. response. Write ("Hello World ");
}
Public bool isreusable {
Get {
Return false;
}
}
}
First, you will find <% @ webhandler Language = "C #" class = "imagehandler" %>. Think about whether an ASP. NET page has something similar. In fact, it indicates that the current file can process an external request. Of course, it won't work.
Next, the key thing is the class created below, which implements a key interface: ihttphandler. This interface indicates the method in which you will process external requests. There is a method and attribute to be implemented. You can write how to handle the request details in the processrequest method, and isreusable indicates whether other requests can use an instance of this class. We can temporarily ignore the isreusable attribute. Focus on the processrequest method. In processrequest, the context parameter is of the httpcontext type. The context object provides reference to internal server objects (such as request, response, session, and server) used to provide services for HTTP requests. That is, we can access several of our server objects.
Now let's look at a simple example.
Please put an image in your own web site folder. My idea is that I first read an image into a binary data and then convert the binary data into an image. You need to create two files. A. aspx file and the. ashx file that we want to use now.
File imagehandler. ashx
<% @ Webhandler Language = "C #" class = "imagehandler" %>
Using system;
Using system. Web;
/// <Summary>
/// This is a general handler without any implementation.
/// </Summary>
Public class imagehandler: ihttphandler {
Public void processrequest (httpcontext context)
{
// Obtain the physical path of the virtual directory.
String Path = context. server. mappath ("");
// Obtain the binary data of the image file.
Byte [] datas = system. Io. file. readallbytes (path + "\ u1513.jpg ");
// Write binary data to the output stream.
Context. response. outputstream. Write (datas, 0, datas. Length );
}
Public bool isreusable {
Get {
Return false;
}
}
}
Default. aspx File
<% @ Page Language = "VB" autoeventwireup = "false" codefile = "default. aspx. VB" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Image id = "image1" runat = "server" imageurl = "~ /Imagehandler. ashx "/> </div>
</Form>
</Body>
</Html>
Note the above Code: <asp: Image id = "image1" runat = "server" imageurl = "~ In/imagehandler. ashx "/> </div>, the imageurl points to the imagehandler. ashx file.
This completes. Source code download
I will continue to explain advanced usage in subsequent articles.
How to read binary images-. ashx General Handler -- 1 --- (reproduced)