最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Dropbox API v2 JavaScript read file - Stack Overflow

programmeradmin4浏览0评论

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 "" 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)

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./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)

Share Improve this question edited Mar 6, 2017 at 19:05 Greg 16.9k2 gold badges38 silver badges45 bronze badges asked Mar 6, 2017 at 9:57 webberpumawebberpuma 7331 gold badge6 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

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 plete 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) {
        ...
    })
发布评论

评论列表(0)

  1. 暂无评论