From b08d7ca9d8a4614b25b6624e32d2850e15b3601a Mon Sep 17 00:00:00 2001 From: zsmj1994 Date: Thu, 30 Jul 2020 11:32:14 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-4881=20refactor:=20bi.multifile=5Feditor?= =?UTF-8?q?=20=E7=AE=80=E5=8C=96=EF=BC=8C=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E7=8A=B6=E6=80=81,=20accept=20=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E4=B8=8E=E6=A0=87=E5=87=86=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 1 + src/base/single/editor/editor.multifile.js | 9 ++++- src/base/single/input/file.js | 45 +++++++++++++++++----- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/changelog.md b/changelog.md index ead4d92ef..5b8352e8c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,6 @@ # 更新日志 2.0(2020-07) +- bi.file文件上传控件accept属性与 [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept) 统一 - BI.OB的on方法返回一个解除监听的函数 - 修复了grid_view执行_unMount时不调用子组件的_unMount的问题 - combo新增belowMouse属性,允许popup在点击处弹出 diff --git a/src/base/single/editor/editor.multifile.js b/src/base/single/editor/editor.multifile.js index f4aa6073b..e5af00a38 100644 --- a/src/base/single/editor/editor.multifile.js +++ b/src/base/single/editor/editor.multifile.js @@ -66,6 +66,10 @@ BI.MultifileEditor = BI.inherit(BI.Widget, { }); }, + _reset: function () { + this.file.reset(); + }, + select: function () { this.file.select(); }, @@ -75,11 +79,12 @@ BI.MultifileEditor = BI.inherit(BI.Widget, { }, upload: function () { + this._reset(); this.file.upload(); }, reset: function () { - this.file.reset(); + this._reset(); } }); BI.MultifileEditor.EVENT_CHANGE = "EVENT_CHANGE"; @@ -87,4 +92,4 @@ BI.MultifileEditor.EVENT_UPLOADSTART = "EVENT_UPLOADSTART"; BI.MultifileEditor.EVENT_ERROR = "EVENT_ERROR"; BI.MultifileEditor.EVENT_PROGRESS = "EVENT_PROGRESS"; BI.MultifileEditor.EVENT_UPLOADED = "EVENT_UPLOADED"; -BI.shortcut("bi.multifile_editor", BI.MultifileEditor); \ No newline at end of file +BI.shortcut("bi.multifile_editor", BI.MultifileEditor); diff --git a/src/base/single/input/file.js b/src/base/single/input/file.js index eb55755c7..df04cd230 100644 --- a/src/base/single/input/file.js +++ b/src/base/single/input/file.js @@ -155,7 +155,7 @@ ); xhr.open("post", handler.url + "&filename=" + _global.encodeURIComponent(handler.file.fileName), true); if (!xhr.upload) { - var rpe = {loaded: 0, total: handler.file.fileSize || handler.file.size, simulation: true}; + var rpe = { loaded: 0, total: handler.file.fileSize || handler.file.size, simulation: true }; rpe.interval = setInterval(function () { rpe.loaded += 1024 / 4; if (rpe.total <= rpe.loaded) { @@ -280,7 +280,7 @@ } } if (isFunction(handler.onload)) { - handler.onload(rpe, {responseText: responseText}); + handler.onload(rpe, { responseText: responseText }); } }, target = ["AjaxUpload", (new Date).getTime(), String(Math.random()).substring(2)].join("_"); @@ -401,6 +401,31 @@ return handler; }; + var r1 = /\.([^.]+)$/; // .png + var r2 = /\/([^/]+)$/; // image/png + + /** + * 校验文件类型是否合法,同时兼容旧版形式 + * @param fileName + * @param fileType + * @returns {boolean} + */ + var fileTypeValidate = function (fileName, fileType) { + var mimes = fileType.split(","); + if (mimes[0] === fileType) { + mimes = (fileType + "").split(";"); + } + return BI.some(mimes, function (index, mime) { + var matches; + if (matches = mime.match(r1)) { + return fileName.toLowerCase().indexOf(matches[1]) > -1; + } + if (matches = mime.match(r2)) { + return matches[1] === "*" ? true : fileName.toLowerCase().indexOf(matches[1]) > -1; + } + }); + }; + BI.File = BI.inherit(BI.Widget, { _defaultConfig: function () { var conf = BI.File.superclass._defaultConfig.apply(this, arguments); @@ -413,7 +438,7 @@ name: "", url: "", multiple: true, - accept: "", /** '*.jpg; *.zip'**/ + accept: "", // .png,.pdf,image/jpg,image/* 等 maxSize: -1 // 1024 * 1024 }); }, @@ -426,6 +451,7 @@ } this.element.attr("name", o.name || this.getName()); this.element.attr("title", o.title || ""); + this.element.attr("accept", o.accept); }, created: function () { @@ -488,13 +514,13 @@ self_.hide(); // hide progress bars and enable input file if (200 > xhr.status || xhr.status > 399) { - BI.Msg.toast(BI.i18nText("BI-Upload_File_Error"), {level: "error"}); + BI.Msg.toast(BI.i18nText("BI-Upload_File_Error"), { level: "error" }); self.fireEvent(BI.File.EVENT_ERROR); return; } var error = BI.some(_wrap.attach_array, function (index, attach) { if (attach.errorCode) { - BI.Msg.toast(attach.errorMsg, {level: "error"}); + BI.Msg.toast(attach.errorMsg, { level: "error" }); self.fireEvent(BI.File.EVENT_ERROR, attach); return true; } @@ -520,16 +546,17 @@ var value = item.fileName || (item.fileName = tempFile.split("\\").pop()), ext = -1 !== value.indexOf(".") ? value.split(".").pop().toLowerCase() : "unknown", size = item.fileSize || item.size; - if (wrap.fileType && -1 === wrap.fileType.indexOf("*." + ext)) { + var validateFileType = fileTypeValidate(value, wrap.fileType); + if (!validateFileType) { // 文件类型不支持 - BI.Msg.toast(BI.i18nText("BI-Upload_File_Type_Error"), {level: "error"}); + 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"}); + BI.Msg.toast(BI.i18nText("BI-Upload_File_Size_Error"), { level: "error" }); self.fireEvent(BI.File.EVENT_ERROR, { errorType: 1, file: item @@ -645,4 +672,4 @@ BI.File.EVENT_PROGRESS = "EVENT_PROGRESS"; BI.File.EVENT_UPLOADED = "EVENT_UPLOADED"; BI.shortcut("bi.file", BI.File); -})(_global.document || {}); \ No newline at end of file +})(_global.document || {});