Jesan Fafon
Jesan Fafon

Reputation: 2304

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.

Upvotes: 3

Views: 2652

Answers (1)

Martijn van der Put
Martijn van der Put

Reputation: 4082

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);

Upvotes: 11

Related Questions