Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

For one of our clients we had to make a media library where visitors can download sitecore media items.

We used an asp imagebutton that executed the following piece of code
     MediaItem mediaItem = ((Sitecore.Data.Fields.ImageField)item.Fields["image"]).MediaItem;

     Stream mediaStream = mediaItem.GetMediaStream();
     long FileSize;

     FileSize = mediaStream.Length;
     byte[] Buffer = new byte[(int)FileSize];
     mediaStream.Read(Buffer, 0, (int)mediaStream.Length);
     mediaStream.Close();

     Response.Clear();
     Response.ContentType = string.Format(mediaItem.MimeType);
     Response.AddHeader("content-disposition", "attachment; filename=" + mediaItem.Name + "." + mediaItem.Extension);
     Response.Cache.SetCacheability(HttpCacheability.NoCache);

     Response.BinaryWrite(Buffer);

     Response.End();

 © Evident Interactive BV