简体   繁体   English

Dropbox API v2 JavaScript读取文件

[英]Dropbox API v2 JavaScript read file

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() . 我想使用Dropbox的JavaScript API(v2)从Dropbox读取文本文件的内容,据我所知,最接近的方法是filesDownload() suppose we have a test.txt file in the root folder with the content 'abc'. 假设我们在根文件夹中有一个test.txt文件,内容为'abc'。 my JavaScript code would look like the following (I use webpack) 我的JavaScript代码如下所示(我使用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 (eg "abc") in the returned object. 但是返回的对象中没有文件内容(例如“abc”)。 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". 相反,当我检查chrome浏览器控制台的网络选项卡时,我可以看到名为“download”的文件,其URL为“ https://content.dropboxapi.com/2/files/download ”,其内容为“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 返回的对象实际上包含一个fileBlob对象

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. 并且您可以使用浏览器的fileReader来获取内容。 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) {
        ...
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM