1
votes

I'm looking at allowing the user to download PDFs which are stored in the Sitecore media library. Basicaly, I'm sending an email with links to the PDFs which the user can download.

Is this possible? And if so, what URL to use?

We are using Sitecore 8.1, .Net MVC and GlassMapper. In glass mapper Ive tried using the Src property of the File in the Model as the href for the link which gives a path to the ashx file but that doesnt appear to work..

From my GlassMapper model:

[SitecoreField("FileForDownload", UrlOptions = Glass.Mapper.Sc.Configuration.SitecoreInfoUrlOptions.AlwaysIncludeServerUrl)]
public virtual File FileForDownload { get; set; }

The resulting FileForDownload.Src is still giving a relative path which may be causing may issues eg "/~/media/XXX/Annual_Report2004Version2.ashx

This may be an outstanding issue in GlassMapper: enter link description here

The anchor in my email is setup using

<a href='" + pub.FileForDownload.Src + "' download>" + pub.Description + "</a>

Ive also tried using the code

 MediaUrlOptions muo = new MediaUrlOptions();
 muo.AlwaysIncludeServerUrl = true;
 var item = Sitecore.Context.Database.GetItem(new ID(pub.Id));
 String url = MediaManager.GetMediaUrl((MediaItem)item, muo);

But thats giving me a url which results in a 404:

http://domainname/-/media/C9F3404515DC46288C5D73E123ACBD5D.ashx

2
show us the code where you are getting media item urlAyman Barhoum
did u try adding html attribute called "download"?Dheeraj Palagiri
Make sure that you have absolute URL rather than relative in your emails. Relative will work on site, but will not in email.Anton

2 Answers

1
votes

If you are using glass mapper, you need to add attribute to the Field to get the absolute URL:

[SitecoreField("File",UrlOptions =Glass.Mapper.Sc.Configuration.SitecoreInfoUrlOptions.AlwaysIncludeServerUrl)]
 public virtual File PDFFile{ get; set; }
-1
votes

I think you need a html attribute called "download" on your anchor tag. This will allow .pdf's to be downloaded rather than opening in new tab or window. If you rendering your anchor using GlassMapper RenderLink try passing in html attribute called download.