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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: <strong>FILESTREAM</strong> with ASP.NET and Silverlight// Start transferring data from the <strong>FILESTREAM</strong> data fileResponse.BufferOutput = false;Response.ContentType = "image/jpeg";BinaryWriter bw = new BinaryWriter(Response.OutputStream);const int bufferSize = 4096;byte[] buffer = new byte[bufferSize];int byteCount = fs.Read(buffer, 0, bufferSize);while (byteCount == bufferSize){bw.Write(buffer, 0, byteCount);byteCount = fs.Read(buffer, 0, bufferSize);}}//Close the filesResponse.End();bw.Close();fs.Close();}catch (Exception ex){//do some error handling}finally{// Commit the transaction and close the connectioncmd.Transaction.Commit();cn.Close();}Listing 5-11:Code to serve images stored in a <strong>FILESTREAM</strong> column.What is interesting and worth noticing here is how we are sending the data back to theweb server. We do this through the ASP.NET Response object. We first set the contenttype to image/jpeg to tell the browser that we are sending back an image. <strong>The</strong>n weaccess the OutputStream property <strong>of</strong> the Response object and write the content <strong>of</strong> the<strong>FILESTREAM</strong> file into it (Listing 5-12).192

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

Saved successfully!

Ooh no, something went wrong!