cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

How to upload multiple files using JavaScript SDK?

How to upload multiple files using JavaScript SDK?

zainulabd786
Explorer | Level 3
Go to solution

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
        });
    }
};
11 Replies 11

Greg-DB
Dropbox Staff
Go to solution

Upload sessions in general are meant for larger files, but you can also use them for smaller files. That makes sense in particular when you have many files to upload at the same time, because the /2/files/upload_session/finish_batch allows you to commit multiple files at once, without causing lock contention between them. You can find more information on that under "Batch Upload" in the data ingress guide Taylor linked to.

MatthieuWdi
New member | Level 2
Go to solution

Hello guys,

 

I searched for a long time and found a way to make it esaylly so I hope my cade can help you.

 

const PHOTO_SAVE_FOLDER = 'C:/Users/matth/Desktop/'

/**
* eventName : foler name where are my files to upload on my computer
* folder : dropbox folder name i when to save my files
*/
 const uploadPhotosOnDropbox = async (eventName,folder) => {
  const errors = [];
  const photosInMyComputer = `${PHOTO_SAVE_FOLDER}${eventName}/${folder}/`
  let photosToUplaod = fs.readdirSync(photosInMyComputer);
  let folderPath = `/Events/${eventName}/${folder}`; // path for dropbox

  // create the folder in dbx,  around with try/catch
  await dbx.filesCreateFolderV2({ path : folderPath, autorename:false}); 

  photosToUplaod.map((photo, idx) =>{
      fs.readFile(`${photosInTheBorne}${photo}`, (err, data) => {
        setTimeout(()=> {
        let path = `/Events/${eventName}/${folder}/${photo}`;
        const contents = data;
        dbx.filesUpload({ path, contents })
      },idx * 2000);
    });
  });

uploadPhotosOnDropbox('09-08-22-MATT', 'persoFolder');
Need more support?
Who's talking

Top contributors to this post

  • User avatar
    MatthieuWdi New member | Level 2
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    zainulabd786 Explorer | Level 3
  • User avatar
    TaylorKrusen Dropbox Staff
What do Dropbox user levels mean?