Dropbox API v2 JavaScript 读取文件 [英] Dropbox API v2 JavaScript read file

查看:26
本文介绍了Dropbox API v2 JavaScript 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Dropbox 的 JavaScript API (v2) 从 Dropbox 读取文本文件的内容,据我所知,最接近的方法是 filesDownload().假设我们在根文件夹中有一个内容为abc"的 test.txt 文件.我的 JavaScript 代码如下所示(我使用 webpack)

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"
    ...
}

但返回的对象中没有文件内容(例如abc").相反,当我检查 chrome 浏览器控制台的网络选项卡时,我可以看到名为下载"的文件,网址为https://content.dropboxapi.com/2/files/download",内容为abc".

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)

推荐答案

哦,我想出了如何获取文件的内容.返回的对象实际上包含一个 fileBlob 对象

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

并且您可以使用浏览器的 fileReader 来获取内容.所以完整的代码如下

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) {
        ...
    })

这篇关于Dropbox API v2 JavaScript 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆