It is not a secret that can deliver media content in a dynamic fashion.
You can do pretty sick things with our MediaRequestHandler (as an example of that, check out ).
Today, however, I wanted to cover a fairly easy aspect of this functionality.

Specifically, the way you can force Media handler to force download of a requested resource, rather than having it being opened in the browser.
I cannot describe the technical challenge any better than explains it.
Basically, it all boils down to sending a specific HTTP header, for example:
**Content-disposition: attachment; filename=fname.ext

**Now good news is that with you are able to control it via configuration and on MIME type basis. What you do is go to web.config and set “forceDownload” parameter to _true _on specific media type, for example, PDF:

<mediaType name="PDF file" extensions="pdf">

<mimeType>application/pdf</mimeType>
** <****forceDownload>true</forceDownload****>
** <sharedTemplate>system/media/unversioned/pdf</sharedTemplate>

<versionedTemplate>system/media/versioned/pdf</versionedTemplate>
</mediaType>
While this is nice and extremely convenient, what if you need more granularity? For example, have certain JPEGs being forced to download? Sitecore’s answer – no problem! Here is what you can do: 1. Create a custom MediaRequestHandler based on default one:
using System.Web;
using Sitecore.Resources.Media;  
  
namespace Sitecore.StarterKit.Media  
{  
 public class ForceDownloadMediaHandler : MediaRequestHandler  
 {  
 public override void ProcessRequest(HttpContext context)  
 {  
 ProcessForceDownload(context);  
 base.ProcessRequest(context);  
 }  
  
 private static void ProcessForceDownload(HttpContext context)  
 {  
 if (context.Request.QueryString["force"] == "1")  
 {  
 var request = MediaManager.ParseMediaRequest(context.Request);  
 if (request != null)  
 {  
 var media = MediaManager.GetMedia(request.MediaUri);  
 if (media != null)  
 {  
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + 

media.MediaData.MediaItem.Name + "." + media.MediaData.Extension + "\"");  
 }  
 }  
 }  
 }  
 }  
}
  1. Compile, place the assembly in /bin folder as usual.

  2. Replace the default MediaRequestHandler with your custom one in web.config:

  3. Depending on the configuration,  under <system.webServer>/<handlers>:

<add verb="*" path="sitecore_media.ashx" type="Sitecore.StarterKit.Media.ForceDownloadMediaHandler, Sitecore.Starterkit" name="Sitecore.MediaRequestHandler" />

  • And/or under <system.web>/<httpHandlers>:

<add verb="*" path="sitecore_media.ashx" type="Sitecore.StarterKit.Media.ForceDownloadMediaHandler, Sitecore.Starterkit" />

  1. When you render a link to a resource, append the “force=1” query string.

  2. Use it:

That’s all, folks!

http://www.site.com/~/media/docs/installguide.ashx?forcedownload=1