Javascript 正在从 Dropbox 下载和读取文件内容

Javascript Downloading and reading file contents from Dropbox

我正在尝试下载我作为测试上传到 Dropbox 的文件。下载功能有效,我也得到了 fileblob,但无法实际读取文件内容

function downloadFile() {
            dbx.filesDownload({path: '/_bk_test/test3.json'})
            .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) {
                console.error(error);
            }); 
}

但我收到此错误作为输出

Promise {<pending>}
VM215:11 TypeError: reader.addEventListener is not a function
    at <anonymous>:5:24

这很奇怪。

但是如果我将 response.fileBlob 存储在全局变量中然后使用 reader 函数,它不会显示 TypeError。但是还是看不懂文件内容

不管怎样,这些都是问题
1. 在函数中,FileReader 抛出异常。
2. 在函数外,FileReader 不显示文件内容。

PS - 在 Cordova 中测试

好的,Cordova 有一个 different API

    function downloadFile() {
        dbx.filesDownload({path: '/_bk_test/test3.json'})
        .then(function(response) {     
            var blob = response.fileBlob;
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("read success");
                console.log(evt.target.result);
            };                
            reader.readAsText(blob);
        })
        .catch(function(error) {
            console.error(error);
        }); 
    }