Force Download and Prompt User to Save Media from the Sitecore Media Library
Recently on Slack Chat a question got asked by fellow MVP Jason Bert about how to prompt a user to save an item from the Sitecore Media Library. Whether a user is prompted to download media depends on a few factors:
- Whether the browser or an associated plugin understands how to process that mime type
- Settings under
mediaLibrary\mediaTypes
in the Sitecore section of config - The response headers sent by the server, and whether
Content-Disposition=attachment
It’s this final header which we need to add ourselves in the Response headers. I shared some code but was pretty surprised when it did not work. Previously in an earlier version of Sitecore I had simply tapped into the getMediaStream
pipeline and then set Content-Disposition=true
header based on a url parameter. Something had obviously changed, or I was going mad. Most likely a combination of both…