SpRestLib

SpRestLib

  • Download
  • Get Started
  • API Documentation
  • SharePoint Guides
  • GitHub

›Recent Posts

Recent Posts

  • Uploading a file to a SharePoint library using JavaScript
  • Downloading a file from SharePoint library using JavaScript and REST API
  • Converting SharePoint 2010 API column names to SharePoint 2013 API column names
  • SharePoint List Unique Permissions REST Query
  • Adding a User to SharePoint Group Using REST with SpRestLib
  • Creating SharePoint jQuery People-Picker with SpRestLib
  • Creating SharePoint List Columns Using REST
  • Uploading a file to a SharePoint library using REST

Downloading a file from SharePoint library using JavaScript and REST API

November 17, 2018

Brent Ely

Howto download a file from SharePoint Library using REST. This technique produces a BLOB that can be streamed/saved directly from a web browser and works with both text and binary files.


Example: Using client browser to download a file

sprLib.file("Path/SomeFileName").get()

Returns: Blob containing the file (either text or binary).

Sample Code: Download a file from SharePoint

// Example: Client-browser code to download file from SharePoint using JavaScript and REST
sprLib.file('SiteAssets/img/sprestlib.png').get()
.then(function(blob){
    var url = (window.URL || window.webkitURL).createObjectURL(blob);
    var link = document.createElement("a");
    link.setAttribute("href", url);
    link.setAttribute("download", _fileName);
    link.style = "visibility:hidden";
    document.body.appendChild(link);
    link.click();
    setTimeout(function(){ document.body.removeChild(link); }, 500);
});

Example

More Information

See File API for a working demo.

Tweet
Recent Posts
  • Example: Using client browser to download a file
  • Sample Code: Download a file from SharePoint
  • More Information
SpRestLib
Docs
Getting Started with SpRestLibSharePoint API ReferenceSharePoint Development GuidesAbout JavaScript Promises
Community
FacebookTwitterPinterestYouTube Channel
More
GitHub IssuesGitHub ProjectSpRestLib on Stack OverflowSite Icons
Copyright © 2019 Brent Ely