You can also easily extend Sitecore to set Content-Disposition
per file. To do this, add a new field on File
template, located here /sitecore/templates/System/Media/Versioned/File
. The best way is to create a new template and make File
template to inherit from it.
The new field can be a checkbox named: Set Content Disposition Inline
.
Then you have to create your own MediaRequestHandler
and set it in Web.config
. You can do that with transform file like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<handlers>
<add verb="*" path="sitecore_media.ashx" type="YourNamespace.MediaRequestHandler, AssemblyName" name="Sitecore.MediaRequestHandler" xdt:Transform="SetAttributes(type)" xdt:Locator="Match(name)" />
</handlers>
</system.webServer>
</configuration>
The last piece of course is MediaRequestHandler
. You have to create a new class and inherit from existing one. This is how your class can looks like:
public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler
{
protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Sitecore.Resources.Media.Media media)
{
if (base.DoProcessRequest(context, request, media))
{
if (context.Response.StatusCode != 304)
{
if (media.MediaData.MediaItem.InnerItem[setContentDispositionFieldId] == "1")
{
var extension = media.Extension;
if (extension.Length > 0 && extension[0] != '.')
{
extension = "." + extension;
}
var fileName = media.MediaData.MediaItem.Name + extension;
context.Response.Headers["Content-Disposition"] = $"inline; filename=\"{fileName}\"";
}
}
return true;
}
return false;
}
}