I would like to use Dropbox's JavaScript API (v2) to read the content of a text file from Dropbox, from what I know the closest method is filesDownload()
. suppose we have a test.txt file in the root folder with the content 'abc'. my JavaScript code would look like the following (I use webpack)
var Dropbox = require('dropbox');
var dbx = new Dropbox({accessToken: '...'});
dbx.filesDownload({path: '/test.txt'})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
an object is actually returned, with the following content
Object {
client_modified: "2017-03-06T06:34:24Z"
content_hash: "f62f4917741f7ed26e883d8193ddf477cde87b99dfbd8d5d8f67eb400087e0b6"
...
}
but there is no file content (e.g. "abc") in the returned object. instead, when I inspect the network tab of the chrome browser console, I can see the file named "download" with the url "https://content.dropboxapi.com/2/files/download" which has the content "abc".
My question is, how can I actually get the content of the file? (Once I can get the content then it is easy to show them in the webpage)
oh, i figure out how to get the content of the file. the returned object actually includes a fileBlob object
Object
client_modified: "2017-03-06T06:34:24Z"
....
fileBlob: Blob
size: 5
type: "application/octet-stream"
__proto__: Blob
...
and you can use browser's fileReader to get the content out. so the complete code would look like the following
var Dropbox = require('dropbox');
var dbx = new Dropbox({accessToken: '...'});
dbx.filesDownload({path: '/test.txt'})
.then(function (response) {
var blob = response.fileBlob;
var reader = new FileReader();
reader.addEventListener("loadend", function() {
console.log(reader.result); // will print out file content
});
reader.readAsText(blob);
})
.catch(function (error) {
...
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With