Instantly share code, notes, and snippets.

What would you like to do?
Creating a dummy file for the beginning of the Large File upload process.
//this method sends the REST request using the SP RequestExecutor.js
function executeAsync(endPointUrl, data, requestHeaders) {
return new Promise((resolve, reject) => {
// remember that utils script we created?
let executor = new SP.RequestExecutor(utils.getSpContaxtUrlParams().appWebUrl);
// Send the request.
url: endPointUrl,
method: "POST",
body: data,
// really important!
binaryStringRequestBody: true,
headers: requestHeaders,
// resolve the promise when the process is complete...
success: offset => resolve(offset),
error: err => reject(err.responseText)
//converts the data into a base64 string
function convertDataBinaryString(data) {
let fileData = '';
let byteArray = new Uint8Array(data);
for (var i = 0; i < byteArray.byteLength; i++) {
fileData += String.fromCharCode(byteArray[i]);
return fileData;
//this is the initial method we call to create a dummy place holder file before overwriting it with the chunks of data...
function createDummaryFile(ctx, fileName, libraryName) {
return new Promise((resolve, reject) => {
// Construct the endpoint - The GetList method is available for SharePoint Online only.
let endpoint = String.format("{0}/_api/sp.appcontextsite(@target)/web/lists/getByTitle('{1}')/rootfolder/files/add(overwrite=true, url='{2}')?@target='{3}'",
utils.getSpContaxtUrlParams().appWebUrl, libraryName, fileName, utils.getSpContaxtUrlParams().hostWebUrl);
const headers = {
"accept": "application/json;odata=verbose"
executeAsync(endpoint, convertDataBinaryString(2), headers)
.then(file => resolve(true))
.catch(err => reject(err.responseText));
/* This is where we declare our entry point - the public facing function upload */
module.exports = {
upload: (file) => {
return new Promise((resolve, reject) => {
let ctx = jsomAppContext();
//first we need to create a dummy file, before we can upload the file in chunks...
createDummaryFile(ctx,, 'Documents').then(result => {
//... more in here later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment