Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
knoppys
6 years agoExplorer | Level 3
Dropbox Javascript SDK - Upload sessions returns an error
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;
}1 Reply
- Greg-DB6 years ago
Dropbox Community Moderator
[Cross-linking for reference: https://github.com/dropbox/dropbox-sdk-js/issues/351 ]
Thanks for the report! I see you also posted this on GitHub. We'll look into it and follow up with you there.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!