Here is the my final solution after the leads from other answers here. I use the media:request event along with the lesser known AddOnSendingHeaders method on Request object that basically gives me a final chance to change headers before IIS sends the request to the client.
public class MediaRequestHandlerChangeFileName
{
: Sitecore.Resources.Media.MediaRequestHandler
public void OnMediaRequest(object sender, EventArgs args)
{
SitecoreEventArgs eventArgs = args protectedas overrideSitecoreEventArgs;
bool DoProcessRequest(HttpContext context, MediaRequest request,mediaRequest Media= media)eventArgs?.Parameters[0] as MediaRequest;
if (mediaRequest == null)
{
return;
bool result }
MediaItem mediaItem = baseMediaManager.DoProcessRequestGetMedia(context, request, mediamediaRequest.MediaUri);.MediaData.MediaItem;
if(mediaItem == null)
{
return;
if (result && IsRightType(media?.MediaData?.MediaItem))
}
HttpRequest request = mediaRequest.InnerRequest;
contextrequest.ResponseRequestContext.Headers["Content-Disposition"] = GetGoodHeaderValue(IsRightType(mediaHttpContext.MediaDataResponse.MediaItem));AddOnSendingHeaders(context =>
{
returnif result;(CheckFileType(mediaItem))
} context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
});
}
}
Then of course, I had to changeadd the webconfigevent handler definition fromto the config:
<add<event verb="*"name="media:request">
path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler" />
to
<add verb="*" path="sitecore_media.ashx" <handler type="MyAssembly.MediaRequestHandlerChangeFileName, MyAssembly" name="Sitecore.MediaRequestHandler" method="OnMediaRequest"/>
</event>
The only downside is that I could not patch the webconfig file and I had to change it, so in the next Sitecore update I have to make sure that my change merged in.