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

Uploading a file to a SharePoint library using REST

March 19, 2018

Brent Ely

Howto upload a file into a SharePoint Library using REST. Both Node.js and client browsers can encode and upload files easily.


Provide a source file using a file picker element in web browsers, or use a file path in Node.js.

There are options for overwriting existing files and to select the destination folder.

Example: Using Node.js to upload a file

// see "nodejs-demo.js" for code on how to acquire a DigestToken `gStrReqDig`
var strFileName = "./someFile.docx";

sprLib.rest({
    url: "_api/web/lists/getByTitle('Documents')/RootFolder/files/add(overwrite=true,url='"+strFileName+"')",
    type: "POST",
    requestDigest: gStrReqDig,
    data: new Buffer( fs.readFileSync(strFileName, 'utf8') )
})
.then((arrResults) => {
    console.log('SUCCESS: "'+ arrResults[0].Name +'" uploaded to: '+ arrResults[0].ServerRelativeUrl );
})
.catch(function(strErr){
    console.error(strErr);
});

Example: Using client browser to upload a file

Given an HTML file picker (<input type="file" id="filePicker">): screen shot 2018-03-18 at 23 38 17

var reader = new FileReader();
reader.readAsArrayBuffer( $('#filePicker')[0].files[0] );
reader.onloadend = function(e){
    var parts = $('#filePicker')[0].value.split('\\');
    var fileName = parts[parts.length - 1];
    var strAjaxUrl = _spPageContextInfo.siteAbsoluteUrl
        + "/_api/web/lists/getByTitle('Site Assets')"
        + "/RootFolder/files/add(overwrite=true,url='"+ fileName +"')";

    sprLib.rest({
        url: strAjaxUrl,
        type: "POST",
        data: e.target.result
    })
    .then(function(arr){
        $('#console').append('SUCCESS: "'+ arr[0].Name +'" uploaded to: '+ arr[0].ServerRelativeUrl +'<br>');
    })
    .catch(function(strErr){
        console.error(strErr);
    });
};
reader.onerror = function(e){
    alert(e.target.error.responseText);
    console.error(e.target.error);
};

More Examples

See examples/sprestlib-demo-file-upload.html for a working demo.

Tweet
Recent Posts
  • Example: Using Node.js to upload a file
  • Example: Using client browser to upload a file
  • More Examples
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