Order-Related Modules for an ASP.NET AJAX Server-Centric Based Online Shopping Website - Web Handler
(Page 4 of 7 )
Here, we adopt the new ASP.NET 2.0 project-the newly-introduced web handler ".ashx" file. According to MSDN, the web handler ".ashx" file works just like an aspx file except WE are one step away from the messy browser level where HTML and C# mix. One reason we would write an .ashx file instead of an .aspx file is that our output is not going to a browser but to an XML-consuming client of some kind. As you have seen, the key piece lies in 'ImageUrl='<%# Eval("PictureID", "../Handler.ashx?Id={0}") %>''. And the following corresponds to the final form of it detected in the browser side.
<img id="ProductView_ctl02_ProductPicture" style="border-width: 0px;
height: 120px; width: 90px;" src="../Handler.ashx?Id=5"/>
Now, let's take a further look into the code behind "Handler.ashx." Since the total code is short, without further ado, we list all of it:
<%@ WebHandler Language="C#" Class="Handler" %>
//using namespaces (omitted)
public class Handler : IHttpHandler {
public void ProcessRequest(HttpContext context){
//context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
Stream outstream = null;
int photoId = -1;
if (context.Request.QueryString["Id"] != null &&
context.Request.QueryString["Id"] != "")
{
photoId = Convert.ToInt32(context.Request.QueryString["Id"]);
outstream = GetPhoto(photoId);
}
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = outstream.Read(buffer, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = outstream.Read(buffer, 0, buffersize);
}
}
public Stream GetPhoto(int photoId) {
SqlConnection myConnection = new SqlConnection
(ConfigurationManager.ConnectionStrings
["SQLCONNECTIONSTRING"].ConnectionString);
SqlCommand myCommand = new SqlCommand
("SELECT [Data] FROM [Pictures] WHERE [PictureID]=@PictureID",
myConnection);
myCommand.CommandType = CommandType.Text;
myCommand.Parameters.Add(new SqlParameter("@PictureID", photoId));
myConnection.Open();
object result = myCommand.ExecuteScalar();
try{
return new MemoryStream((byte[])result);
}
catch (ArgumentNullException e) {
return null;
}
finally{
myConnection.Close();
}
}
public bool IsReusable {
get { return false; }
}
}
In the above code, the core component is the public method, namelyProcessRequest. When the parameterized URL is passed to it, it accepts the parameter to a variable named "photoId," as follows.
photoId = Convert.ToInt32(context.Request.QueryString["Id"]);
Next, in the helper function GetPhoto, I directly use SQL operations to fetch the image data from the database (of course you can move the module elsewhere if you prefer). The subsequent lines of code in the ProcessRequest methodabstracts the binary image data and then writes it to the specified context, as follows:
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = outstream.Read(buffer, 0, buffersize);
}
The reason that we leveraged a whileloop is just to more efficiently deal with the data-you can follow your own calculating algorithms.
As for the "product.aspx.cs" background file, there is nothing more particular to notice but the common data source binding operations, as follows.
ProductView.DataSource = dr;
ProductView.DataBind();
So, you see, the .jpg (or .png, .gif) files stored in the database are finally displayed at the specified position on the web page!
Author's Note: Here you should be careful to refer to the path of the ".ashx" files, or else you won't be able to see the image.
Next: Buying Goods >>
More ASP.NET Articles
More By Xianzhong Zhu