Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This recursive function uploads chunks to a file in a document library
//the final REST call is made to get the file information after it has been fully uploaded (especially the file list item id)
function getFileInformation(libraryPath, fileName, resolve, reject) {
let endpoint = String.format("{0}/_api/sp.appcontextsite(@target)/web/getfilebyserverrelativeurl(@libraryPath)/ListItemAllFields?@target='{3}'&@libraryPath='/sites/assetdatabase/{1}/{2}'",
utils.getSpContaxtUrlParams().appWebUrl, libraryPath, fileName, utils.getSpContaxtUrlParams().hostWebUrl);
const headers = {
"Accept": "application/json; odata=verbose"
executeAsync(endpoint, '', headers).then(fileListItem => {
console.log('fetching file information');
const items = fileListItem.body ? fileListItem.body : fileListItem;
const listItem = JSON.parse(items);
//..and we are done.
}).catch(err => {
function uploadFile(result, id, libraryPath, fileName, chunks, index, byteOffset, chunkPercentage, resolve, reject) {
//we slice the file blob into the chunk we need to send in this request (byteOffset tells us the start position)
const data = convertFileToBlobChunks(result, byteOffset, chunks[index]);
if (byteOffset === 0) {
//at the beginning of the upload set the message and starting percentage (0%)
setLoaderMessage(true, 0);
//upload the chunk to the server using REST, using the unique upload guid as the identifier
uploadFileChunk(id, libraryPath, fileName, chunks[index], data, byteOffset).then(
value => {
const isFinished = index === chunks.length - 1;
if (!isFinished) {
//the response value is a string of JSON (ugly) which we need to consume to find the offset
const response = typeof value.body !== 'undefined' ? JSON.parse(value.body) : '';
//depending on the position in the upload, the response string (JSON) can differ!
if (typeof response.d.StartUpload !== 'undefined') {
byteOffset = Number.parseInvariant(response.d.StartUpload);
} else if (typeof response.d.ContinueUpload !== 'undefined') {
byteOffset = Number.parseInvariant(response.d.ContinueUpload);
index += 1;
const percentageComplete = isFinished ? 100 : Math.round((index * chunkPercentage));
// progress indication
setLoaderMessage(true, percentageComplete);
console.log(percentageComplete + '%');
//More chunks to process before the file is finished, continue
if (index < chunks.length) {
uploadFile(result, id, libraryPath, fileName, chunks, index, byteOffset, chunkPercentage, resolve, reject);
} else {
//check in the file and then resolve the file information back to the caller
//checkinMajorVersion(libraryPath, fileName).then(() => {
//when there was a checkin needed
getFileInformation(libraryPath, fileName, resolve, reject);
//}).catch(err => {
//no checkin was necessary
//getFileInformation(libraryPath, fileName, resolve, reject);
).catch(err => { console.log('Error in uploadFileChunk! '); window.Erz = err; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment