cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Whether you want to work on being more organized, your productivity or you want to use AI to make your life easier, we’ve got something for you right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dropbox Javascript SDK - Upload sessions returns an error

Dropbox Javascript SDK - Upload sessions returns an error

knoppys
Explorer | Level 3

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 1

Greg-DB
Dropbox Staff

[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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?