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

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

问题描述

我想使用Dropbox的JavaScript API(v2)从Dropbox读取文本文件的内容,据我所知,最接近的方法是 filesDownload()。假设我们在根文件夹中有一个test.txt文件,内容为'abc'。我的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浏览器控制台的网络选项卡时,我可以看到名为download的文件,其中包含网址 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天全站免登陆