Hi All
Hope you can help me here, long running project.
So Im using the Dropbox SDK from github in a wordpress plugin.
A simple form which uploads files directly to a dropbox folder.
Im using the same exmaple as the JS example on the repo but I keep getting the following error when uploading chunks of large files. Always after the first upload.
Error in call to API function "files/upload_session/append:2": HTTP header "Dropbox-API-Arg": cursor: missing required field 'session_id'
Here is the code Im using.
var knpvDropbox = this;
const UPLOAD_FILE_SIZE_LIMIT = 50 * 1024 * 1024;
var ACCESS_TOKEN = 'tokenblahblahblah';
var dbx = new Dropbox.Dropbox({ accessToken: ACCESS_TOKEN });
var fileInput = document.getElementById('fileSelect');
var file = fileInput.files[0];
if (file.size < UPLOAD_FILE_SIZE_LIMIT) { // File is smaller than 150 Mb - use filesUpload API
dbx.filesUpload({path: knpvDropbox.folderpath + '/' +file.name, contents: file})
.then(function(response) {
new DropboxList(knpvDropbox.folderpath);
jQuery('.message-container').html(`Your upload is complete!`);
jQuery('.modal-container').delay(2000).fadeOut();
formvalidation.knpv_validate_dropbox();
})
.catch(function(error) {
console.error(error);
});
} else { // File is bigger than 150 Mb - use filesUploadSession* API
const maxBlob = 8 * 1000 * 1000; // 8Mb - Dropbox JavaScript API suggested max file / chunk size
var workItems = [];
var offset = 0;
while (offset < file.size) {
var chunkSize = Math.min(maxBlob, file.size - offset);
workItems.push(file.slice(offset, offset + chunkSize));
offset += chunkSize;
}
const task = workItems.reduce((acc, blob, idx, items) => {
if (idx == 0) {
// Starting multipart upload of file
return acc.then(function() {
return dbx.filesUploadSessionStart({ close: false, contents: blob})
.then(response => response.session_id)
});
} else if (idx < items.length-1) {
// Append part to the upload session
return acc.then(function(sessionId) {
var cursor = { session_id: sessionId, offset: idx * maxBlob };
return dbx.filesUploadSessionAppendV2({ cursor: cursor, close: false, contents: blob }).then(() => sessionId);
});
} else {
// Last chunk of data, close session
return acc.then(function(sessionId) {
var cursor = { session_id: sessionId, offset: file.size - blob.size };
var commit = { path: knpvDropbox.folderpath + '/' +file.name, mode: 'add', autorename: true, mute: false };
return dbx.filesUploadSessionFinish({ cursor: cursor, commit: commit, contents: blob });
});
}
}, Promise.resolve());
task.then(function(result) {
new DropboxList(knpvDropbox.folderpath);
jQuery('.message-container').html(`Your upload is complete!`);
jQuery('.modal-container').delay(2000).fadeOut();
formvalidation.knpv_validate_dropbox();
}).catch(function(error) {
console.error(error);
});
}
return false;
}