In our solution we have some particular cases where we want to provide a "download media" link instead of a straightforward media link.
We don't want to force download for any particular file types, we want to provide a URL that forces download. So setting the <forceDownload>
element in <mediaLibrary><mediaTypes>
isn't an option.
We implemented this some years ago in 6.4, and we're now on 7.2 (which has some extended media options - for instance, we use the AcceptRangeRequests
setting to allow partial requests)
Our solution was to build a custom media handler that was inserted into the pipeline. The media handler called the base handler ProcessRequest(context)
method and then set the content-disposition header to "attachment"
We need to call ProcessRequest prior to setting the content-disposition header, as it fills the context object with data we needed, particularly the filename which needs to be set in the header.
This seemed to be working OK, mostly, but we've discovered that when media is served from the server disc cache the headers are appended too late, giving us an error in the logs - "can't append headers after response is sent". This doesn't occur if the media is fetched from the database (we assume the response must be sent to the client differently if media is served from cache, possibly just handed off to IIS?).
We're hoping to avoid replacing ProcessRequest with custom code.
We can also see that ProcessRequest contains it's own methods for determining and setting headers, and that there's a
Sitecore.Resources.Media.MediaStream ForceDownload
method.
Are there properties or methods on the context object we can work with to do this, prior to calling ProcessRequest(context)? Or is there some way of modifying how headers are set (a pipeline?) within ProcessRequest?