17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: <strong>FILESTREAM</strong> with ASP.NET and SilverlightTo illustrate a slightly more advanced ASP.NET concept, for this lab we willimplement a generic handler, rather than repurposing a web form as a handler.Right-click on the Presentation project, and select Add | New Item and in theAdd New Item dialog box, select the Generic Handler template and give the newitem the name GetItemImage.ashx. At the top <strong>of</strong> the file, add namespace importstatements for the DataAcess and System.IO namespaces.We will write our code in ProcessRequest(), which is the method called by theASP.NET runtime if the handler is invoked. <strong>The</strong> code is shown in Listing 5-16.public void ProcessRequest(HttpContext context){context.Response.ContentType = System.Net.Mime.MediaTypeNames.Image.Jpeg;context.Response.BufferOutput = false;}ItemData data = new ItemData();MemoryStream ms = data.GetItemImage(context.Request.QueryString["itemID"]);if (ms != null && ms.Length > 0){context.Response.AddHeader("content-length", ms.Length.ToString());// Reset the current position in the MemoryStreamms.Position = 0;// .NET 4.0 and later method onlyms.CopyTo(context.Response.OutputStream);}Listing 5-16:ProcessRequest() method code.This code contains no references to <strong>SQL</strong>, connection strings, or other database access.210

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!