ASP. NET 3.5 core programming learning notes (43): Image Viewer for compiling HTTP processing programs

Source: Internet
Author: User

Image Viewer processing program

To speed up processing, IIS directly processes some static resources in Web applications without calling ISAPI extensions. These static files include images and HTML files. If you directly request a GIF or JPG file from a browser, IIS will obtain the specified resource, correctly set the content type of the Response Buffer, and output it to the byte stream of the file.

What if the browser address points to a virtual directory containing multiple images? In this case, IIS cannot identify the folder content, but may only return the list of files.

So how can I display the preview of this directory in a browser by pointing to this virtual directory address?

Design of HTTP processing program for Image Viewer

First, you need to determine how to make IIS know our intention? You can append a specific Endpoint after the folder name so that IIS can send the request to ASP. NET, and ASP. NET can preview the image. In other words, the idea is to bind the Image Viewer handler to a specific endpoint (such as folder. axd. Fixed endpoints do not necessarily point to existing and deployed resources. You can append folder. axd to the folder name. The address is as follows:

Http://www.contoso.com/images/folder.axd

The handler needs to extract the folder name in the URL and select the image to preview.

In ASP. NET, The axd extension is usually used to reference special services. Trace. axd used for tracking and webresource. axd used to insert scripts and resources are typical cases of using this extension.

Implementation of the Image Viewer HTTP processing program

The Image Viewer handler returns a multi-column table containing all the images in the specified folder. The structure of this class is as follows:

class PictureViewerInfo
{
public PictureViewerInfo()
{
DisplayWidth = 200;
ColumnCount = 3;
}

public int DisplayWidth;
public int ColumnCount;
public string FolderName;
}

public class PictureViewerHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
PictureViewerInfo info = GetFolderInfo(context);
string html = CreateOutput(info);

//Output the data
context.Response.Write("
context.Response.Write("Picture Web Viewer");
context.Response.Write("</title>
context.Response.Write(html);
context.Response.Write("</body>
}

public bool IsReusable
{
get { return true; }
}
...
}

To obtain the actual path, you only need to remove the "folder. axd" string from the URL and all the slashes and backslashes. Then, we map the URL of the folder to the server path and process it through. NET Framework.

private ArrayList GetAllImages(string path)
{
string[] fileTypes = { "*.bmp", "*.gif", "*.jpg", "*.png" );
ArrayList Images = new ArrayList();
DiectoryInfo di = new DirectoryInfo(path);
foreach(string ext in fileTypes)
{
FileInfo[] files = di.GetFiles(ext);
if(files.Length > 0)
images.AddRange(files);
}
return images;
}

Preview the output image:

string CreateOutputForFolder(PictureViewerInfo info)
{
ArrayList images = GetAllImages(info.FolderName);
Table t = new Table();

int index = 0;
bool moreImages = true;
while(moreImages)
{
TableRow row = new TableRow();
t.Rows.Add(row);
for(int i = 0; i < info.ColumnCount; i++)
{
TableCell cell = new TableCell();
row.Cells.Add(cell);

//Create the image
Image img = new Image();
FileInfo fi = (FileInfo)images[index];
img.ImageUrl = filName;
img.Width = Unit.Pixel(info.DisplayWidth);

//Wrap the image in an anchor so tha a larger image is
//shown when the user clicks
HtmlAnchor a = new HtmlAnchor();
a.Href = new HtmlAnchor(); a.Controls.Add(img);
cell.Controls.Add(a);

//Check whether there are more images to show
index++;
moreImages = (index < images.Count);
if(!moreImage)
break;
}
}    string html = Utils.RenderControlAsString(t);
    return html;
}

We can make the handler accept some optional query string parameters (such as width and number of columns ). The process for processing URL query strings is as follows:

PictureViewerInfo info = new PictureViewInfo();
object p1 = context.Request.Params["Width"];
object p1 = context.Request.Params[""];

The process for processing URL query strings is as follows:

PictureViewerInfo = new PictureViewerINfo();
object p1 = context.request.Params["Width"];
object p2 = context.request.Params["Cols"];
if(p1 != null)
Int32.TryParse((string)p1, out info.DisplayWidth);
if(p1 != null)
Int32.TryParse((string)p2, out info.ColumnCount);

The registration process is very simple. Add the following script to Web. config:

<add verb="*" path="folder.axd" type="Core35.Components.PictureViewerHandler, Core35Lib" />

Place the generated assembly in GAC and insert this script into global web. config. All applications on the current computer can share this function.

Example:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.