Skip to main content
tried to add syntax highlighting hints
Source Link
Richard Seal
  • 20.8k
  • 5
  • 44
  • 79
public class ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters?[0] as MediaRequest;


        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;

        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }

}
public class ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters?[0] as MediaRequest;
        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;
        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }
}
<event name="media:request">
        <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
      </event>
<event name="media:request">
    <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
</event>
public class ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters?[0] as MediaRequest;


        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;

        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }

}
<event name="media:request">
        <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
      </event>
public class ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters?[0] as MediaRequest;
        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;
        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }
}
<event name="media:request">
    <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
</event>
minor code fix
Source Link

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 ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters[0]Parameters?[0] as MediaRequest;


        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;

        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }

}

Then of course, I had to add the event handler to the config:

<event name="media:request">
        <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
      </event>

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 ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters[0] as MediaRequest;


        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;

        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }

}

Then of course, I had to add the event handler to the config:

<event name="media:request">
        <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
      </event>

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 ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters?[0] as MediaRequest;


        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;

        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }

}

Then of course, I had to add the event handler to the config:

<event name="media:request">
        <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
      </event>
The initial solution did work very occasionally on some files, since the response stream should immediately close after the base method call returns.
Source Link

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.

Here is the my final solution after the leads from other answers here:

public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler
        {
            protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Media media)
            {
                bool result = base.DoProcessRequest(context, request, media);
            
                if (result && IsRightType(media?.MediaData?.MediaItem))
                    context.Response.Headers["Content-Disposition"] = GetGoodHeaderValue(IsRightType(media.MediaData.MediaItem));
    
                 return result;
            }
        }

Then of course, I had to change the webconfig handler definition from:

<add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler" />

to

<add verb="*" path="sitecore_media.ashx" type="MyAssembly.MediaRequestHandler, MyAssembly" name="Sitecore.MediaRequestHandler" />

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.

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 ChangeFileName
{
    public void OnMediaRequest(object sender, EventArgs args)
    {
        SitecoreEventArgs eventArgs = args as SitecoreEventArgs;

        MediaRequest mediaRequest = eventArgs?.Parameters[0] as MediaRequest;


        if (mediaRequest == null)
        {
            return;
        }

        MediaItem mediaItem = MediaManager.GetMedia(mediaRequest.MediaUri).MediaData.MediaItem;

        if(mediaItem == null)
        {
            return;
        }

        HttpRequest request = mediaRequest.InnerRequest;

        request.RequestContext.HttpContext.Response.AddOnSendingHeaders(context =>
        {
            if (CheckFileType(mediaItem))
                context.Response.Headers["Content-Disposition"] = FixFileName(context.Response.Headers["Content-Disposition"]));
        });
    }

}

Then of course, I had to add the event handler to the config:

<event name="media:request">
        <handler type="MyAssembly.ChangeFileName, MyAssembly" method="OnMediaRequest"/>
      </event>
Source Link
Loading