Đang chuẩn bị liên kết để tải về tài liệu:
ASP.NET 4 Unleased - p 151
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Working with the HTTP Runtime { const string connectionStringName = “Images”; public void ProcessRequest(HttpContext context) { // Don’t buffer response context.Response.Buffer = false; // Get file name string fileName = VirtualPathUtility.GetFileName(context.Request.Path); // Get image from database string conString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand(“SELECT Image FROM Images WHERE FileName=@FileName”, con); cmd.Parameters.AddWithValue(“@fileName”, fileName); using (con) { con.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); if (reader.Read()) { int bufferSize = 8040; byte[] chunk = new byte[bufferSize]; long retCount; long startIndex = 0; retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize); while (retCount == bufferSize) { context.Response.BinaryWrite(chunk); startIndex +=. | 1474 CHAPTER 31 Working with the HTTP Runtime const string connectionStringName Images public void ProcessRequest HttpContext context Don t buffer response context.Response.Buffer false Get file name string fileName VirtualPathUtility.GetFileName context.Request.Path Get image from database string constring WebConfigurationManager.ConnectionStrings connectionStringName .ConnectionString SqlConnection con new SqlConnection conString SqlCommand cmd new SqlCommand SELECT Image FROM Images WHERE FileName @FileName con cmd.Parameters.AddWithValue @fileName fileName using con con.Open SqlDataReader reader cmd.ExecuteReader CommandBehavior.SequentialAccess if reader.Read int bufferSize 8040 byte chunk new byte bufferSize long retCount long startindex 0 retCount reader.GetBytes 0 startindex chunk 0 bufferSize while retCount bufferSize context.Response.BinaryWrite chunk startindex bufferSize retCount reader.GetBytes 0 startindex chunk 0 bufferSize byte actualChunk new Byte retCount - 1 Buffer.BlockCopy chunk 0 actualChunk 0 int retCount - 1 context.Response.BinaryWrite 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 31.17 includes an httpHandlers section that associates the .gif .jpeg and .jpg extensions with the Image handler. LISTING 31.17 Web.Config xml version 1.0 configuration connectionStrings add name Images connectionString Data Source . SQLExpress Integrated Security True AttachDBFileName DataDirectory ImagesDB.mdf User Instance True connectionStrings system.web httpHandlers add path .gif verb type AspNetUnleashed.ImageHandler validate false add path .jpeg verb type AspNetUnleashed.ImageHandler validate false add path .jpg verb type AspNetUnleashed.ImageHandler validate false httpHandlers system.web configuration When you register a .