You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

zainulabd786's avatar
zainulabd786
Explorer | Level 3
6 years ago

How to upload multiple files using JavaScript SDK?

Hi, I am using Javascript SDK to upload multiple files at once. I have a dropbox business account enabled and the folder where I am trying to upload the files is a shared folder.

I want to upload the files to

root > Clients > Somefolder

Clients and Somefolder are the folders created by me.

Here's my code:

const uploadClientData = async (req, res) => {
	try {
		const files = req.files
		const { dbxUser } = req;
		const args = {
			contents: files,
			path: '/Clients/Wise',
			mode: 'add',
			autorename: true,
			mute: false,
			strict_conflict: false
		}
		await dbxUser.filesUpload(args);

		res.status(200).json({message: "Uploaded"});
	} catch (error) {
		console.log(error)
		res.status(500).json({
			error: error
        });
	}
}

And here my dropbox Initialization

module.exports = async (req, res, next) => {
    try {

        let dbxTeam = new DropboxTeam({ fetch: fetch });
        let access_token;

        dbxTeam.setClientId("XXXXXXXXXX");
        dbxTeam.setClientSecret("XXXXXXXXXX");
        
        if(req.body.code){
            access_token = await dbxTeam.getAccessTokenFromCode('http://localhost:3000/authentication', req.body.code);
        } else {
            let team_member_id = req.userData.team_member_id
            access_token = await helpers.getAdminDetails({team_member_id}, "access_token")
            access_token = access_token[0].access_token;
        }
        
        dbxTeam.setAccessToken(access_token);
        let { admin_profile } = await dbxTeam.teamTokenGetAuthenticatedAdmin()

        let dbxUser = new Dropbox({
            accessToken: access_token, 
            selectUser: admin_profile.team_member_id, 
            pathRoot: JSON.stringify({".tag": "root", "root": "5619027360"}) 
        })
        
        req.dbxTeam = dbxTeam,
        req.dbxUser = dbxUser
        next();
    } catch (error) {
        return res.status(500).json({
            message: 'Dropbox Init failed',
            error: error
        });
    }
};
  • Greg-DB's avatar
    Greg-DB
    6 years ago

    I see, thanks for clarifying. The Dropbox API doesn't offer a way to upload multiple files in one call, but I'll pass this along as a feature request. I can't promise if or when that might be implemented though. 

    That being the case, you will need to loop through your files and call filesUpload once per file. Apologies I don't have better news for you!

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    The filesUpload method is the right way to upload multiple files, by calling it once per file.

    Can you elaborate on what exactly isn't working for you so we can provide some more specific guidance? For instance, is some part of your code giving you an unexpected error? Please share any error or unexpected output you're getting.

    • zainulabd786's avatar
      zainulabd786
      Explorer | Level 3
      const args = {
      	contents: files,
      	path: '/Clients/Wise',
      	mode: 'add',
      	autorename: true,
      	mute: false,
      	strict_conflict: false
      }
      await dbxUser.filesUpload(args);

       Here the 'files' parameter that I just passed is an array of multiple files. 

      So my question is, Do I need to loop through the files array and upload files one by one. Because this code doesn't work this way. but if I change the above code to :

      const args = {
      	contents: files[0].buffer,
      	path: '/Clients/Wise',
      	mode: 'add',
      	autorename: true,
      	mute: false,
      	strict_conflict: false
      }

      This works perfectly. But I don't want to loop through the array. Can't I pass the array to the contents parameter

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        I see, thanks for clarifying. The Dropbox API doesn't offer a way to upload multiple files in one call, but I'll pass this along as a feature request. I can't promise if or when that might be implemented though. 

        That being the case, you will need to loop through your files and call filesUpload once per file. Apologies I don't have better news for you!

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,910 PostsLatest Activity: 3 days ago
333 Following

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 or Facebook.

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!