Last active
February 13, 2017 11:25
-
-
Save beckettkev/fbce99c70d02a2b462a60b3a8e159811 to your computer and use it in GitHub Desktop.
Creating a dummy file for the beginning of the Large File upload process.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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. | |
executor.executeAsync({ | |
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, file.name, '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