Search code examples
sitecoresitecore6sitecore-media-library

Sitecore - Create File Download Link


In sitecore, I have items that have a "File" field.

When I use code such as the following:

var bioDoc = item["BiographyDocument"];

I get a string that looks like this:

<file mediaid=\"{3891941C-AC19-4DEB-AD5D-2E4FA4DD2E07}\" src=\"~/media/3891941CAC194DEBAD5D2E4FA4DD2E07.ashx\" />

How do I go from this string to a MediaItem and then a download link? I've spend quite a bit of time on this and couldn't find the answer anywhere.

Thanks.


Solution

  • Use the MediaManager to get the MediaItem url:

    Sitecore.Data.Fields.FileField field =  
                 (Sitecore.Data.Fields.FileField)myItem.Fields["BiographyDocument"];
    string url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(field.MediaItem);