【问题标题】:dropzone.js Change display unitsdropzone.js 更改显示单位
【发布时间】:2015-03-05 21:02:07
【问题描述】:

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

【问题讨论】:

    标签: dropzone.js


    【解决方案1】:

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

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

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

    希望对你仍然有用。

    【讨论】:

    • 感谢您的建议!
    【解决方案2】:

    这更类似于 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(而不是 Dropzone 默认返回的 0.3 MB

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

    【讨论】:

      【解决方案3】:

      这段代码对我有用:

      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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        • 1970-01-01
        • 2019-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多