1

我正在尝试将我作为测试上传的文件下载到 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 - 在科尔多瓦进行测试

4

1 回答 1

1

好的,Cordova 有不同的 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);
        }); 
    }
于 2018-06-01T13:23:12.427 回答