dropzone.js更改显示单位

时间:2015-03-05 21:02:08

标签: dropzone.js

有谁知道是否可以更改上传文件的单位显示?我上传了一个600 MB的文件,显示屏显示0.6 Gib ...这不是真正的用户友好。我查看了网站上的说明,除了如何将filesizeBase从1000更改为1024之外,找不到任何其他内容。

3 个答案:

答案 0 :(得分:1)

我有类似的需求,因为我必须始终在KB上显示单位。我在dropzone.js中找到了一个名为filesize的函数,我只是在我自己的代码中用下一个函数覆盖了它:

Dropzone.prototype.filesize = function(size) {
  var selectedSize = Math.round(size / 1024);
  return "<strong>" + selectedSize + "</strong> KB";
};

我认为你必须覆盖相同的功能,但要根据你的需要进行调整。

我希望对你有用。

答案 1 :(得分:0)

这与Dropzone中包含的现有文件大小功能更为相似(但更为冗长)。

Dropzone.prototype.filesize = function (bytes) {
    let selectedSize = 0;
    let selectedUnit = 'b';
    let units = ['kb', 'mb', 'gb', 'tb'];
    
    if (Math.abs(bytes) < this.options.filesizeBase) {
        selectedSize = bytes;
    } else {
        var u = -1;
        do {
            bytes /= this.options.filesizeBase;
            ++u;
        } while (Math.abs(bytes) >= this.options.filesizeBase && u < units.length - 1);

        selectedSize = bytes.toFixed(1);
        selectedUnit = units[u];
    }

    return `<strong>${selectedSize}</strong> ${this.options.dictFileSizeUnits[selectedUnit]}`;
}

示例:

339700字节-> 339.7 KB (而不是Dropstrong默认返回的 0.3 MB

来源:https://stackoverflow.com/a/14919494/1922696

答案 2 :(得分:0)

这段代码对我有用:

Dropzone.prototype.filesize = function (bytes) {
    let selectedSize = 0;
    let units = ['B', 'KB', 'MB', 'GB', 'TB'];

    var size = bytes;
    while (size > 1000) {
        selectedSize = selectedSize + 1;
        size = size/1000;
    }

    return "<strong>" + Math.trunc(size * 100)/100 + "</strong> " + units[selectedSize];
}

我要除以1000,因为否则我得到1010 KB,而不是1.01 MB。