From fdbca99476a5f6f0c11c6e6bdde33ff19b04b71b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E4=BA=9A=E5=B0=94-IT?= <136627746+Diyar-IT@users.noreply.github.com> Date: Fri, 26 Apr 2024 16:29:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(upload):=20=E4=BF=AE=E5=A4=8D=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=97=B6=E8=83=BD?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=90=8C=E6=A0=B7=E6=96=87=E4=BB=B6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#1757)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复BUG * Update upload.js * 增加开展文件信息方法,修复小问题 * chore(upload): 修改 upload.util.parseSize 的 JSDoc 参数类型 Co-authored-by: morning-star <26325820+Sight-wcg@users.noreply.github.com> * chore(upload): 更正引用参数错误 Co-authored-by: morning-star <26325820+Sight-wcg@users.noreply.github.com> * chore(upload): 更正 JSDoc --------- Co-authored-by: 贤心 <3277200+sentsim@users.noreply.github.com> Co-authored-by: morning-star <26325820+Sight-wcg@users.noreply.github.com> --- src/modules/upload.js | 89 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/src/modules/upload.js b/src/modules/upload.js index 8ebcf718..e5cea0b2 100644 --- a/src/modules/upload.js +++ b/src/modules/upload.js @@ -600,6 +600,66 @@ layui.define(['lay', 'layer'], function(exports){ elemFile.after(''+ value +''); }; + /** + * 判断文件是否加入排队 + * @param {File} file + * @return {boolean} + */ + var checkFile = function (file) { + var result = true; + layui.each(that.files, function (index, item) { + result = !(item.name === file.name); + if(!result) return true; + }); + return result; + } + + /** + * 扩展文件信息 + * @template {File | FileList} T + * @param {T} obj + * @return {T} + */ + var extendInfo = function (obj) { + + var extInfo = function (file) { + //文件扩展名 + file.ext = file.name.substr(file.name.lastIndexOf('.') + 1).toLowerCase(); + // 文件大小 + file.sizes = upload.util.parseSize(file.size); + // 可以继续扩展 + } + + //FileList对象 + if (obj instanceof FileList) { + layui.each(obj, function (index, item) { + extInfo(item); + }); + } else { + extInfo(obj); + } + + return obj; + } + + /** + * 检查获取文件 + * @param {FileList} files + * @return {Array|FileList} + */ + var getFiles = function (files) { + files = files || []; + if (!files.length) return []; + if (!that.files) return extendInfo(files); + var result = []; + layui.each(files, function (index, item) { + if (checkFile(item)) { + result.push(extendInfo(item)); + } + }); + return result; + } + // 点击上传容器 options.elem.off('upload.start').on('upload.start', function(){ var othis = $(this); @@ -620,7 +680,7 @@ layui.define(['lay', 'layer'], function(exports){ }) .off('upload.drop').on('upload.drop', function(e, param){ var othis = $(this); - var files = param.originalEvent.dataTransfer.files || []; + var files = getFiles(param.originalEvent.dataTransfer.files); othis.removeAttr('lay-over'); setChooseFile(files); @@ -631,7 +691,7 @@ layui.define(['lay', 'layer'], function(exports){ // 文件选择 that.elemFile.on('change', function(){ - var files = this.files || []; + var files = getFiles(this.files); if(files.length === 0) return; @@ -678,6 +738,31 @@ layui.define(['lay', 'layer'], function(exports){ options.elem.data(MOD_INDEX, options.id); }; + /** + * 上传组件辅助方法 + */ + upload.util = { + /** + * 文件大小处理 + * @param {number | string} size -文件大小 + * @param {number} [precision] - 数值精度 + * @return {string} + */ + parseSize: function (size, precision) { + precision = precision || 2; + if (null == size || !size) { + return '0'; + } + var unitArr = ["Bytes", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"]; + var index; + var formatSize = typeof size === 'string' ? parseFloat(size) : size; + index = Math.floor(Math.log(formatSize) / Math.log(1024)); + size = formatSize / Math.pow(1024, index); + size = size % 1 === 0 ? size : parseFloat(size.toFixed(precision));//保留的小数位数 + return size + unitArr[index]; + } + } + // 记录所有实例 thisModule.that = {}; // 记录所有实例对象