From c7d3a88e70e857c38ed08c7f575bdd3e18d0c036 Mon Sep 17 00:00:00 2001 From: windy <1374721899@qq.com> Date: Tue, 1 Dec 2020 15:10:51 +0800 Subject: [PATCH] =?UTF-8?q?BI-76381=20bi.file=E6=94=AF=E6=8C=81=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=9C=80=E5=A4=A7=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 3 + src/base/single/editor/editor.multifile.js | 1 + src/base/single/input/file.js | 67 +++++++++++++--------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/changelog.md b/changelog.md index 114a474062..b91d3fb21e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,7 @@ # 更新日志 +2.0(2020-12) +- bi.file支持限制上传文件数 + 2.0(2020-11) - bi.file上传文件errorMsg默认调用国际化 - 修复了文本标签text传递空字符串后显示value值的问题 diff --git a/src/base/single/editor/editor.multifile.js b/src/base/single/editor/editor.multifile.js index e5af00a38b..3a8db85ae4 100644 --- a/src/base/single/editor/editor.multifile.js +++ b/src/base/single/editor/editor.multifile.js @@ -31,6 +31,7 @@ BI.MultifileEditor = BI.inherit(BI.Widget, { multiple: o.multiple, accept: o.accept, maxSize: o.maxSize, + maxLength: o.maxLength, title: o.title }); this.file.on(BI.File.EVENT_CHANGE, function () { diff --git a/src/base/single/input/file.js b/src/base/single/input/file.js index 2d220e7dd3..221c60787d 100644 --- a/src/base/single/input/file.js +++ b/src/base/single/input/file.js @@ -208,7 +208,7 @@ attachO.attach_type = "image"; } attachO.filename = handler.file.fileName; - if (handler.maxlength == 1) { + if (handler.maxLength == 1) { handler.attach_array[0] = attachO; // handler.attach_array.push(attachO); } else { @@ -271,7 +271,7 @@ } catch (e) { attachO.filename = handler.file.fileName; } - if (handler.maxlength == 1) { + if (handler.maxLength == 1) { handler.attach_array[0] = attachO; } else { handler.attach_array[current] = attachO; @@ -444,7 +444,8 @@ url: "", multiple: true, accept: "", // .png,.pdf,image/jpg,image/* 等 - maxSize: -1 // 1024 * 1024 + maxSize: -1, // 1024 * 1024 + maxLength: -1 // 无限制, 与multiple配合使用 }); }, @@ -463,7 +464,7 @@ var self = this, o = this.options; // create the noswfupload.wrap Object // wrap.maxSize 文件大小限制 - // wrap.maxlength 文件个数限制 + // wrap.maxLength 文件个数限制 var _wrap = this.wrap = this._wrap(this.element[0], o.maxSize); // fileType could contain whatever text but filter checks *.{extension} // if present @@ -545,30 +546,39 @@ var self = this; event.add(wrap.dom.input, "change", function () { event.del(wrap.dom.input, "change", arguments.callee); - for (var input = wrap.dom.input.cloneNode(true), i = 0, files = F(wrap.dom.input); i < files.length; i++) { - var item = files.item(i); - var tempFile = item.value || item.name; - var value = item.fileName || (item.fileName = tempFile.split("\\").pop()), - ext = -1 !== value.indexOf(".") ? value.split(".").pop().toLowerCase() : "unknown", - size = item.fileSize || item.size; - var validateFileType = fileTypeValidate(value, wrap.fileType); - if (!validateFileType) { - // 文件类型不支持 - BI.Msg.toast(BI.i18nText("BI-Upload_File_Type_Error"), { level: "error" }); - self.fireEvent(BI.File.EVENT_ERROR, { - errorType: 0, - file: item - }); - } else if (wrap.maxSize !== -1 && size && wrap.maxSize < size) { - // 文件大小不支持 - BI.Msg.toast(BI.i18nText("BI-Upload_File_Size_Error"), { level: "error" }); - self.fireEvent(BI.File.EVENT_ERROR, { - errorType: 1, - file: item - }); - } else { - wrap.files.unshift(item); - // BI.Msg.toast(value); + var input = wrap.dom.input.cloneNode(true); + var files = F(wrap.dom.input); + if (wrap.maxLength !== -1 && wrap.maxLength < files.length) { + BI.Msg.toast(BI.i18nText("BI-Upload_File_Count_Error"), { level: "error" }); + self.fireEvent(BI.File.EVENT_ERROR, { + errorType: 2 + }); + } else { + for (var i = 0; i < files.length; i++) { + var item = files.item(i); + var tempFile = item.value || item.name; + var value = item.fileName || (item.fileName = tempFile.split("\\").pop()), + ext = -1 !== value.indexOf(".") ? value.split(".").pop().toLowerCase() : "unknown", + size = item.fileSize || item.size; + var validateFileType = fileTypeValidate(value, wrap.fileType); + if (!validateFileType) { + // 文件类型不支持 + BI.Msg.toast(BI.i18nText("BI-Upload_File_Type_Error"), { level: "error" }); + self.fireEvent(BI.File.EVENT_ERROR, { + errorType: 0, + file: item + }); + } else if (wrap.maxSize !== -1 && size && wrap.maxSize < size) { + // 文件大小不支持 + BI.Msg.toast(BI.i18nText("BI-Upload_File_Size_Error"), { level: "error" }); + self.fireEvent(BI.File.EVENT_ERROR, { + errorType: 1, + file: item + }); + } else { + wrap.files.unshift(item); + // BI.Msg.toast(value); + } } } wrap.files.length > 0 && self.fireEvent(BI.File.EVENT_CHANGE, { @@ -602,6 +612,7 @@ name: input.name, // name to send for each file ($_FILES[{name}] in the server) // maxSize is the maximum amount of bytes for each file maxSize: o.maxSize ? o.maxSize >> 0 : -1, + maxLength: o.maxLength, files: [], // file list // remove every file from the noswfupload component