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

时间:2018-06-01 12:55:37

标签: javascript blob dropbox filereader dropbox-js

我正在尝试将我上传的文件下载到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中进行测试

1 个答案:

答案 0 :(得分:0)

好的,科尔多瓦有一个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);
        }); 
    }