Pages

Thursday, October 10, 2013

Control file download using a HttpHandler - without exposing the files location

This will show you how to make a HttpHandler which can retrieve files without exposing the files location.
You can add additional logic such as download count or access restriction.

First things first, you'll need to setup your HttpHandler, I've called mine GetFile.cs:
namespace Sample.HttpHandlers
{
    public class GetFile : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            // The logic goes here
        }
    }
}
Remember to add the handler in your web.config, additional information can be found at microsoft support.
<add name="GetFile" verb="*" path="GetFile.axd" type="Sample.HttpHandlers.GetFile"/>