SharePoint 2013: force a document link to download

by zebsadiq 1. April 2015 17:36

Forcing a link to download in this day in age should be an easy task. It is, until you have to make it cross browser compliant and have to cater for pre-HTML5 browsers.

Firstly, I just want to point out that if you are only concerned about HTML5 browsers, you simply need to add the ‘download’ attribute to your link. This forces the browsers to try to download the linked file rather than trying to load it. Here is an example:

<a href="/Documents/MYDOCTODOWNLOAD.doc" download>DOWNLOAD HERE</a>

In the scenario discussed here, we are assuming the following restrictions:

  • The solution should support IE8 (not HTML5 compliant) .
  • It should work for SharePoint 2013.
  • It should avoid using code behind (C#) in order to make it cloud friendly.

The problem: In order for the download mechanism to trigger, the response from the server side needs to contain certain metadata in the response header. This is near impossible to tackle without code behind.

However, we can utilise SharePoint’s own mechanism for forcing files to download. When you reverse engineer SharePoint 2013’s code for forcing file downloads, you discover that it is using a page acting as a request handler called download.aspx to manipulate the response headers server side.  Here is my code for utilising this functionality.

<a href="/Documents/MYDOCTODOWNLOAD.doc" 
onclick="DownloadClickHandler(this,event);return false;">
DOWNLOAD HERE
</a>

<script>
function DownloadClickHandler(sender,e) {
	// Get the link to the document 
	var linkToDocument = $(sender).attr('href');
	linkToDocument = encodeURIComponent(linkToDocument);
	// Get the url for the current page
	var thisPageUrl = encodeURIComponent(document.URL);
	// Formulate the site collection url
	var siteCollectionUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
	// Create a redirect to the download.aspx file passing the link to document as a parameter
	var downloadRedirect = siteCollectionUrl + "_layouts/15/download.aspx?SourceUrl="  + linkToDocument + "&FldUrl=&Source=" + thisPageUrl;	
	// Open in the link in a new window
	window.open(downloadRedirect);
}
</script>

The code above works out the path to download.aspx. It should work even if you are not in the root site collection as the site collection path is formulated by using some CSOM variables loaded by sp.js. We open the formulated link to the document download in a new window just in case there is a problem with the download link mechanism in which case the error will be shown in a new window. Otherwise the use’s current window would redirect to the error page and their user context may be lost.  The onclick event of the link has to return false, otherwise the browser tries to redirect the page to the url defined in the href. This works in IE8 onwards, FireFox, Chrome and Safari.

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Calendar

<<  December 2024  >>
MoTuWeThFrSaSu
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar