tailieunhanh - ASP.NET 4 Unleased - p 151

Working with the HTTP Runtime { const string connectionStringName = “Images”; public void ProcessRequest(HttpContext context) { // Don’t buffer response = false; // Get file name string fileName = (); // Get image from database string conString = [connectionStringName].ConnectionString; SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand(“SELECT Image FROM Images WHERE FileName=@FileName”, con); (“@fileName”, fileName); using (con) { (); SqlDataReader reader = (); if (()) { int bufferSize = 8040; byte[] chunk = new byte[bufferSize]; long retCount; long startIndex = 0; retCount = (0, startIndex, chunk, 0, bufferSize); while (retCount == bufferSize) { (chunk); startIndex +=. | 1474 CHAPTER 31 Working with the HTTP Runtime const string connectionStringName Images public void ProcessRequest HttpContext context Don t buffer response false Get file name string fileName Get image from database string constring connectionStringName .ConnectionString SqlConnection con new SqlConnection conString SqlCommand cmd new SqlCommand SELECT Image FROM Images WHERE FileName @FileName con @fileName fileName using con SqlDataReader reader if int bufferSize 8040 byte chunk new byte bufferSize long retCount long startindex 0 retCount 0 startindex chunk 0 bufferSize while retCount bufferSize chunk startindex bufferSize retCount 0 startindex chunk 0 bufferSize byte actualChunk new Byte retCount - 1 chunk 0 actualChunk 0 int retCount - 1 actualChunk From the Library of Wow eBook Creating HTTP Handlers 1475 public bool IsReusable get return true 31 After you create a class that implements the IHttpHandler interface you need to register the class in the web configuration file. The web configuration file in Listing includes an httpHandlers section that associates the .gif .jpeg and .jpg extensions with the Image handler. LISTING xml version configuration connectionStrings add name Images connectionString Data Source . SQLExpress Integrated Security True AttachDBFileName DataDirectory User Instance True connectionStrings httpHandlers add path .gif verb type validate false add path .jpeg verb type validate false add path .jpg verb type validate false httpHandlers configuration When you register a .