From 9a6b85f1a24da3025f998c40f8c3f8470aba6606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:22:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20upload=20=E5=A4=9A?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=A8=A1=E5=BC=8F=E4=B8=AD=EF=BC=8C=E6=AD=A3?= =?UTF-8?q?=E5=9C=A8=E4=B8=8A=E4=BC=A0=E4=B8=AD=E7=9A=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BC=9A=E8=A2=AB=E5=86=8D=E6=AC=A1=E8=A7=A6=E5=8F=91=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/upload.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/modules/upload.js b/src/modules/upload.js index 21bab6ef..68fb69c3 100644 --- a/src/modules/upload.js +++ b/src/modules/upload.js @@ -52,6 +52,7 @@ layui.define(['lay','layer'], function(exports){ var ELEM_IFRAME = 'layui-upload-iframe'; var ELEM_CHOOSE = 'layui-upload-choose'; var ELEM_DRAG = 'layui-upload-drag'; + var UPLOADING = 'UPLOADING'; // 构造器 var Class = function(options){ @@ -218,16 +219,37 @@ layui.define(['lay','layer'], function(exports){ var request = function(sets){ var formData = new FormData(); + // 删除正在上传中的文件队列 + var removeUploaded = function(index, file) { + if (file[UPLOADING]) { + delete items[index]; + return true; + } + }; + // 追加额外的参数 layui.each(options.data, function(key, value){ value = typeof value === 'function' ? value() : value; formData.append(key, value); }); - // 添加 file 到表单域 - sets.unified ? layui.each(items, function(index, file){ - formData.append(options.field, file); - }) : formData.append(options.field, sets.file); + /* + *添加 file 到表单域 + */ + + // 是否统一上传 + if (sets.unified) { + layui.each(items, function(index, file){ + if (removeUploaded(index, file)) return; + file[UPLOADING] = true; + formData.append(options.field, file); + }); + } else { // 逐一上传 + if (removeUploaded(sets.index, sets.file)) return; + formData.append(options.field, sets.file); + } + + sets.file[UPLOADING] = true; // 上传中的标记 // ajax 参数 var opts = {