前言就不多说了,直接言归正传!!!
// 使用layui.all.js 中的代码示范(格式化后 lines=4323 or search=请求上传接口出现异常)
layui.each(a, function (e, a) {
var r = new FormData;
r.append(l.field, a), layui.each(l.data, function (e, i) {
i = "function" == typeof i ? i() : i, r.append(e, i)
}), i.ajax({
url: l.url,
type: "post",
data: r,
contentType: !1,
processData: !1,
dataType: "json",
headers: l.headers || {},
success: function (i) {
t++, d(e, i), u()
},
error: function () {
n++, o.msg("请求上传接口出现异常"), m(e), u()
}
})
})
//self begin(合并资源上传)
var r_my = new FormData;
var e1 = '';//每个文件的标识,为修改文件状态准备
layui.each(a, function (e, a) {
e1 = e1 + (e + '&');
r_my.append(l.field, a)
})
layui.each(l.data, function (e1, i) {
i = "function" == typeof i ? i() : i, r_my.append(e1, i)
})
i.ajax({
url: l.url,
type: "post",
data: r_my,
contentType: !1,
processData: !1,
dataType: "json",
headers: l.headers || {},
success: function (i) {
var arr = e1.split('&');//遍历每个文件唯一标识修改状态
for (var j = 0; j < arr.length - 1; j++) {
t++, d(arr[j], i), u()
}
},
error: function () {
var arr = e1.split('&');
for (var j = 0; j < arr.length - 1; j++) {
n++, o.msg("请求上传接口出现异常"), m(arr[j]), u()
}
}
})
以上呢,就基本满足各位的需求了,但是,可能还有其它的地方影响到,没有发现(每个文件的其它操作),毕竟人家写了一堆都是针对单个文件的,所以如果大家发现有什么问题或者修改好了的,欢迎在评论区发出,以便后来人参考
@RequestParam("file") MultipartFile[] files
@RequestMapping(value = "/uploadZfzl")
@ResponseBody
public String uploadZfzl(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) throws Exception {
String result = "上传失败";
IFService fService = new IFServiceImpl();
if (null != files && files.length > 0) {
String curPageFileId = request.getParameter("curPageFileId");
Map fileMap = new HashMap<>();
List fileDtoList = new ArrayList<>();
for (MultipartFile file : files) {
String entryName = file.getOriginalFilename();
String indexName = entryName.substring(entryName.lastIndexOf(".") + 1);
if ("jpg".equalsIgnoreCase(indexName) || "png".equalsIgnoreCase(indexName) || "pdf".equalsIgnoreCase(indexName)) {//jpg,png,pdf
byte[] fileBytes = file.getBytes();
DzdaDto fileDto = new DzdaDto();
fileDto.setFileType(indexName);
fileDto.setFileName(entryName);
fileDto.setFileContent(fileBytes);
fileDto.setWjlxDm("03");
fileDtoList.add(fileDto);
}
}
fileMap.put("curPageFileId", curPageFileId);
fileMap.put("fileDtoList", fileDtoList);
try {
fService .uploadZfzl(fileMap);
result = "上传成功";
} catch (Exception e) {
log.error("上传失败!", e);
result = "上传失败";
}
}
return result;
}
, before: function (obj) { //choose 回调之后、done/error 回调之前触发
var fileTableNum = $('#fbzlPre tr').length;
if (fileTableNum > 4) {
layer.alert('请检查附报资料的数量', {icon: 2, skin: 'layer-ext-moon'});
die;//无法return,使用报错结束上传
// return;
}
}