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 JavaScript

December 10, 2018

Brent Ely

Howto upload a file into a SharePoint Library using SpRestLib. The upload() method accepts an ArrayBuffer from both Node.js and client browsers.


The sprLib.folder().upload() method accepts a filename and file data as an ArrayBuffer.

Provide file data via an HTML file picker or via fs in Node.

  • There are options for security tokens and overwriting existing files
  • See Folder API for a complete documentation

Using client browser to upload a file

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

Sample Code

// STEP 1: Use FilePicker to read file
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];

    // STEP 2: Upload file to SharePoint
    sprLib.folder('/sites/dev/Documents').upload({
        name: fileName,
        data: e.target.result,
        overwrite: true
    })
    .then(function(objFile){
        console.log('SUCCESS: `'+ objFile.Name +'` uploaded to: `'+ objFile.ServerRelativeUrl +'`' );
    })
    .catch(function(strErr){
        console.error(strErr);
    });
});

Working Demo

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

howto-sprestlib-upload file

Using Node.js to upload a file

As node runs outside of an aspx page, you'll need to provide an authorization RequestDigest to SharePoint (see node-js-demo.js for a working example of getting auth tokens from SharePoint using node).

Sample Code

sprLib.folder(gStrFilePath).upload({
    name: 'jeff_teper_secret_plan.docx',
    data: fs.readFileSync('./docs/jeff_teper_secret_plan.docx'),
    requestDigest: gStrReqDig,
    overwrite: true
})
.then((objFile) => {
    console.log('SUCCESS: `'+ objFile.Name +'` uploaded to: `'+ objFile.ServerRelativeUrl +'`' );
})
.catch((strErr) => {
    console.error(strErr);
});

Working Demo

See example/nodejs-demo.js for a working demo.

Tweet
Recent Posts
  • Using client browser to upload a file
    • Sample Code
    • Working Demo
  • Using Node.js to upload a file
    • Sample Code
    • Working Demo
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