How to Intercept the download of a Media file

casper.rasmussen/ June 12, 2016/ Content Types and Properties, Episerver CMS/ 2 comments

Intercepting the download process of MediaData in Episerver can be helpful when e.g. tracking of downloads or lead generation features are requested by clients. Previously our Episerver teams has numerous times been requested to implement scenarios that ensures valuable visitor information are collected before a file-download is processed.

I won’t cover the actual generation or download tracking, but I will instead focus the interception of the download-process. It enables you to implement any customized logic that is tailored for the need of your solution.

Episerver does indicate how something like this can be achieved in their documentation. Multiple posts on Episerver World though tells me that it hasn’t been explained thoroughly enough. So I prepared a code-snippet that helps clarifying this topic.

It is important that Episerver is aware of the implementation. Implementing IRenderTemplate and detailing that it acts as a HttpHandler, by using the TemplateDescriptor feature in Episerver, is essential. Relying on out-of-the-box features such as BlobHttpHandler and DownloadMediaRouter is helpful and ensures that the implementation is kept to the bare minimum. I feel that remaining details are self-explanatory and that this example acts as a good foundation for complex file-processing features in Episerver.

Hopefully this small post provided you with the information you needed in order to intercept the download process in Episerver.


  1. I tried this code and method GetBlob called twice on one download.
    Do you know why this happens and how to avoid it?

    1. Hi.

      Thanks for asking. I would not expect GetBlob to be executed twice – so that’s something that requires some digging. It’s not something related to the code included in my blog post, so I would instead start investigating the URL of any executing requests and get an idea of the origin. That may lead you in the direction of a solution.

      Unfortunately I don’t have the reasoning to the behavior you are seeing.

      Casper Aagaard Rasmussen

Leave a Comment

Your email address will not be published. Required fields are marked *

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>