Cordova中的文件大小功能 [英] File size function in cordova

查看:64
本文介绍了Cordova中的文件大小功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个简单的问题,但是我正在搜索两天,却找不到答案.

Maybe it's an easy question but it's two days i'm searching and i can't find an answer.

我正在科尔多瓦开发一个文件管理器,只是为了学习科尔多瓦插件文件.

I'm developing a file manager in cordova just to learn the cordova-plugin-file.

我已经使用此功能获得了文件夹中的文件列表:

i've obtained a list of files in a folder using this function:

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dirEntry) {

    var directoryReader = dirEntry.createReader();
    directoryReader.readEntries(function(entries) {

        var row;

        for (i=0; i<entries.length; i++) {

            row = entries[i];
            console.log(row.name);

        }

    }, function(error) {
        console.log(error);
    });

}, function(error) {
    console.log(error);
});

现在我想在文件名旁边打印文件大小,因此我编写了此函数,该函数为我提供了文件大小:

Now i want to print the filesize next to the file name so i wrote this function which gives me the file size:

function getFileSize(file) {

    window.resolveLocalFileSystemURL(file, function (fileEntry) {
        fileEntry.file(function(fileObj) {
            var bytes = fileObj.size;
            var i = Math.floor(Math.log(bytes) / Math.log(1024)),
            sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
            returnSize = (bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
        });
    });

}

有没有办法在for周期内实现类似的目的?

Is there a way to achieve something like this inside the for cycle?

var row;
var size;

for (i=0; i<entries.length; i++) {

    row = entries[i];
    size = getFileSize(cordova.file.externalDataDirectory + row.name);
    console.log(row.name);

}

我知道问题出在文件插件的异步特性上,但是我找不到解决方案/什么是实现我在此处尝试编写代码的正确语法.

I know the problem is the async nature of the file plugin but i can't find a solution / what is the correct syntax to achieve what i'm trying to code here.

推荐答案

不,您不能以这种方式这样做,由于调用的异步性质,您需要回调.然后即可渲染.

No, you can't do it this way, you need a callback due to the async nature of the call. Then you can render.

我相信您在entrys数组中具有所有文件名/路径.像这样循环遍历该数组并将大小保存到另一个数组中

I believe you have all file names/paths in the entries array. Loop over that array and save sizes in another array, doing something like this

var size_array = [];
var total = 0;

for(var i = 0; i < entries.length; i++){
     getFileSize(i);
}


function getFileSize(index) {

    window.resolveLocalFileSystemURL(entries[index], function (fileEntry) {
        fileEntry.file(function(fileObj) {
            var bytes = fileObj.size;
            var i = Math.floor(Math.log(bytes) / Math.log(1024)),
            sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
            size_array[index] = (bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
            total ++;
            if(total === entries.length){
                filesReady();
            }
        });
    });

}


function filesReady(){
   for(var i = 0; i < entries.length; i++){
      entries[i] <-- your file
      size_array[i] <-- its size
   }
}

这篇关于Cordova中的文件大小功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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