views:

95

answers:

2

I have a ASP .NET load balanced application (webservice and website). It runs on SQL server. I need to be able to provide large files for download. However, because of the load balancing situation, the files are stored in the SQL database as opposed to the file system. BITS seems to be the best approach. I have full control of the client. However, i don't know how to configure BITS to read the file from the database. I know how to write the C# code for that, but i don't know how to get BITS to hook into it as opposed to reading the file from the file system.

Any ideas?

A: 

Have a look at 2008 Books Online OpenSqlFilestream. That API has examples that may help you.

Jack Knows Jack
+1  A: 

You can create a custom http handler by implementing System.Web.IHttpHandler. The ProcessRequest(HttpContext context) method is where you will write your file retrieval code from the database. Since BITS operates with range requests you will need to parse the value of context.Request.Headers["Range"] to get the start and end bytes requested. In the ProcessRequest you can read the binary from the database using the SqlCommand.ExecuteReader(CommandBehavior.SequentialAccess) method and set the resulting binary in context.Response.OutputStream. Remember to call context.Response.Flush() at the end.

The custom HttpHandler will serve a particular file extension (e.g. '.file'). This is what needs to be done in IIS:

Both IIS Versions

  1. Add to section in in web.config:

IIS 6.0

  1. Add .file (application/x-zip-compressed) extension as MIME type for the website.

  2. Add Application Extension (Website Properties  Virtual Directory  Configuration  Mappings)

Extension: .file Executable Path(s): %windir%\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll %windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll

IIS 7.0

  1. Add to section in in web.config:

  1. Add to section in in web.config:

        <mimeMap fileExtension=".file" mimeType="application/x-zip-compressed" />
    

Hope that's enough to get you started.

Shady