Created
June 7, 2026 18:33
-
-
Save hishaamn/876ecdbd5d38b44c7c19723bf3198830 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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