How to upload file in dropbox using nodejs
Posted By : Chandan Kumar Singh | 20-May-2016
In this blog I am going to explain how to upload a file in dropbox, In my previous blog http://www.oodlestechnologies.com/blogs/How-to-create-a-folder-in-dropbox-using-nodejs we have created directory in which we are going to upload a video file. You can upload any file (pdf,document,jpg etc).
As we have two files called dropbox.js and drop.js, We are writing the methods to do real work in dropbox.js and calling this method from node.js
For uploading a file we will create a method called createFIle in dropbox.js file.
createFile: function(path, content,cb) {
options = {
method: "POST",
url: 'https://content.dropboxapi.com/2/files/upload',
headers: {
"Content-Type": "application/octet-stream",
"Authorization": "Bearer " + access_token,
"Dropbox-API-Arg": "{\"path\": \"/test/testDir/sample.mp4\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}",
},
body:content
};
request(options, function(err, res, body) {
cb(err, res, body);
});
},
And in drop.js file add this piece of code.
fs.readFile('/home/chandan/sample.mp4',function(err,content) { //for reading the local file
if(!err){
api.createFile('test/testDir/sample.mp4',content , function(err, res, body){
if(!err){
console.log("body: ",body);
}
});
}
});
}
});
Here we are first reading the local file called sample.mp4 and calling the createFile method which is taking the destination folder with file name as path.
Reference: https://www.dropbox.com/developers/documentation/http/documentation
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Kumar Singh
Chandan is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate. He loves technologies like Blockchain and IoT.