I have written a custom http handler. I have done this by writing a class which implements the IHttphandler.
Inside that class I have code like this,
Take out both the Flush() and Close() call. You really don't need them. Once your handler is done, it'll exit, and ASP.NET will handle closing the request.
Besides, Flush() should be used when you're streaming content to the client (adding parts to the response stream in blocks). You don't need to use it with TransmitFile().