Skip to content

Instantly share code, notes, and snippets.

@hishaamn
Created June 7, 2026 18:33
Show Gist options
  • Select an option

  • Save hishaamn/876ecdbd5d38b44c7c19723bf3198830 to your computer and use it in GitHub Desktop.

Select an option

Save hishaamn/876ecdbd5d38b44c7c19723bf3198830 to your computer and use it in GitHub Desktop.
using Sitecore.Resources.Media;
using Sitecore.Data.Items;
using System.Web;
namespace MyProject.Foundation.Media
{
    public class CustomMedia : Media
    {
        private const string ForceDownloadFieldName = "Force Download";
        public override MediaStream GetStream()
        {
            MediaItem mediaItem = this.MediaData.MediaItem;
            if (IsForceDownloadEnabled(mediaItem))
            {
                this.SetAttachmentDisposition(HttpContext.Current.Response, mediaItem.Name, mediaItem.Extension);
            }
            return base.GetStream();
        }
        private bool IsForceDownloadEnabled(Item item)
        {
            var field = item.Fields[ForceDownloadFieldName];
            return field != null && (Sitecore.Data.Fields.CheckboxField)field;
        }
        private void SetAttachmentDisposition(HttpResponse response, string name, string extension)
        {
            string filename = $"{name}.{extension}";
            response.Headers["Content-Disposition"] = $"attachment; filename=\"{filename}\"";
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment