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.

Discuss Dropbox Developer & API

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

How to use filesDownloadZip() to download zip file in javascript SDK

How to use filesDownloadZip() to download zip file in javascript SDK

zainulabd786
Explorer | Level 3

I want to download the folder in my dropbox as a zip file. I am using JavaScript SDK. I know there's filesDownloadZip() available for that. 

I am able to get the Binary data from API, But still unable to download the file.

I am using Node/Express Server and React on front-end.

Here's my Backend code that's interacting with api:

 

const downloadZip = async (req, res) => {
	/**
	 * folder_id: String
	 * team_member_id: String <Id of the member who shared the folder>
	 */
	try{

		if(!req.body.folder_id)
			return res.status(400).json({error: "This endpoint expects folder_id to be passed."});

		if(!req.body.team_member_id)
			return res.status(400).json({error: "This endpoint expects team_member_id to be passed."});

		const { dbxUser } = req;
		const { folder_id } = req.body;
		const data = await dbxUser.filesDownloadZip({path: folder_id})
		res.status(200).json({data});
	} catch(error) {
		console.log(error)
		res.status(500).json({
			error
                });
	}	
}

And Here's my Front-end code:

 

onDownloadBtnClick = async () => {
        try{
            const { folder_id, shared_by: team_member_id } = this.state.data;
            const { data } = await axios.post(`${sharedRoute}/downloadZip`, {folder_id, team_member_id});
            window.open(JSON.stringify(data.fileBinary));
        } catch(error){
            console.error(error)
        }
    }

I've also posted this question on StackOverflow https://stackoverflow.com/questions/57024827/how-to-download-file-from-buffer-stored-in-json-object

 

How can I download the zip file out of this data?

 

 

1 Reply 1

Greg-DB
Dropbox Staff

If you want to get a zip of a folder on Dropbox, the 'filesDownloadZip' method you already found is the right method. (If you only wanted a single file, you would use the 'filesDownload' method.

In either case, when performing a download call like this in Node, if the call succeeds the resulting data is in the 'fileBinary' field. (If running in browser JavaScript, it would be in the 'fileBlob' field.)

In the case of using 'filesDownloadZip', that data is the actual raw data for the zip file containing the requested folder. 

It looks like you have the Dropbox part of this itself working, so I don't think I can offer to much help with the part that you're currently stuck on. Exactly what you do with that returned data and how you serve it back to the user is up to and will depend on the web framework you're using in this case.

That said, I wouldn't expect to use '.json()' or 'JSON.stringify' on the returned data, as it's not an object you necessarilly want to convert into a JSON string. Also, 'window.open' doesn't seem right as that would expect a URL, but the data isn't a URL. 

You should look into how to send a Buffer from your Node backend to the client. For example, here's a question on StackOverflow that looks similar: https://stackoverflow.com/questions/25206141/having-trouble-streaming-response-to-client-using-expre...

Need more support?
Who's talking

Top contributors to this post

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