From e62e8e372cf4368f139da191666d3e5e8c62e781 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期五, 25 二月 2022 09:08:56 +0800
Subject: [PATCH] 组件ImageUpload支持多图同时选择上传
---
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java | 55 +++++++++++++++++++++++++--
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java | 18 +++++++++
ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java | 5 --
ruoyi-ui/src/components/ImageUpload/index.vue | 15 ++++++-
4 files changed, 81 insertions(+), 12 deletions(-)
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
index acd12cc..d51d61d 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
@@ -1,5 +1,7 @@
package com.ruoyi.web.controller.common;
+import java.util.ArrayList;
+import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
@@ -8,6 +10,7 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.RuoYiConfig;
@@ -24,6 +27,7 @@
* @author ruoyi
*/
@RestController
+@RequestMapping("/common")
public class CommonController
{
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@@ -31,13 +35,15 @@
@Autowired
private ServerConfig serverConfig;
+ private static final String FILE_DELIMETER = ",";
+
/**
* 閫氱敤涓嬭浇璇锋眰
*
* @param fileName 鏂囦欢鍚嶇О
* @param delete 鏄惁鍒犻櫎
*/
- @GetMapping("common/download")
+ @GetMapping("/download")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
{
try
@@ -64,9 +70,9 @@
}
/**
- * 閫氱敤涓婁紶璇锋眰
+ * 閫氱敤涓婁紶璇锋眰锛堝崟涓級
*/
- @PostMapping("/common/upload")
+ @PostMapping("/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception
{
try
@@ -77,8 +83,47 @@
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
- ajax.put("fileName", fileName);
ajax.put("url", url);
+ ajax.put("fileName", fileName);
+ ajax.put("newFileName", FileUtils.getName(fileName));
+ ajax.put("originalFilename", file.getOriginalFilename());
+ return ajax;
+ }
+ catch (Exception e)
+ {
+ return AjaxResult.error(e.getMessage());
+ }
+ }
+
+ /**
+ * 閫氱敤涓婁紶璇锋眰锛堝涓級
+ */
+ @PostMapping("/uploads")
+ public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
+ {
+ try
+ {
+ // 涓婁紶鏂囦欢璺緞
+ String filePath = RuoYiConfig.getUploadPath();
+ List<String> urls = new ArrayList<String>();
+ List<String> fileNames = new ArrayList<String>();
+ List<String> newFileNames = new ArrayList<String>();
+ List<String> originalFilenames = new ArrayList<String>();
+ for (MultipartFile file : files)
+ {
+ // 涓婁紶骞惰繑鍥炴柊鏂囦欢鍚嶇О
+ String fileName = FileUploadUtils.upload(filePath, file);
+ String url = serverConfig.getUrl() + fileName;
+ urls.add(url);
+ fileNames.add(fileName);
+ newFileNames.add(FileUtils.getName(fileName));
+ originalFilenames.add(file.getOriginalFilename());
+ }
+ AjaxResult ajax = AjaxResult.success();
+ ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
+ ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
+ ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
+ ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
return ajax;
}
catch (Exception e)
@@ -90,7 +135,7 @@
/**
* 鏈湴璧勬簮閫氱敤涓嬭浇
*/
- @GetMapping("/common/download/resource")
+ @GetMapping("/download/resource")
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
index dd1bcc4..69c7cc4 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
@@ -256,4 +256,22 @@
}
return strFileExtendName;
}
+
+ /**
+ * 鑾峰彇鍚嶇О
+ *
+ * @param fileName 璺緞鍚嶇О
+ * @return 娌℃湁鏂囦欢璺緞鐨勫悕绉�
+ */
+ public static String getName(String fileName)
+ {
+ if (fileName == null)
+ {
+ return null;
+ }
+ int lastUnixPos = fileName.lastIndexOf('/');
+ int lastWindowsPos = fileName.lastIndexOf('\\');
+ int index = Math.max(lastUnixPos, lastWindowsPos);
+ return fileName.substring(index + 1);
+ }
}
diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java
index ec2e0b8..6712a72 100644
--- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java
+++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleUtils.java
@@ -125,10 +125,7 @@
int count = StringUtils.countMatches(packageName, ".");
if (count > 1)
{
- if (!StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR))
- {
- return false;
- }
+ return StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR);
}
return true;
}
diff --git a/ruoyi-ui/src/components/ImageUpload/index.vue b/ruoyi-ui/src/components/ImageUpload/index.vue
index 978576f..bf04c5b 100644
--- a/ruoyi-ui/src/components/ImageUpload/index.vue
+++ b/ruoyi-ui/src/components/ImageUpload/index.vue
@@ -1,6 +1,7 @@
<template>
<div class="component-upload-image">
<el-upload
+ multiple
:action="uploadImgUrl"
list-type="picture-card"
:on-success="handleUploadSuccess"
@@ -70,6 +71,8 @@
},
data() {
return {
+ number: 0,
+ uploadList: [],
dialogImageUrl: "",
dialogVisible: false,
hideUpload: false,
@@ -124,9 +127,14 @@
},
// 涓婁紶鎴愬姛鍥炶皟
handleUploadSuccess(res) {
- this.fileList.push({ name: res.fileName, url: res.fileName });
- this.$emit("input", this.listToString(this.fileList));
- this.loading.close();
+ this.uploadList.push({ name: res.fileName, url: res.fileName });
+ if (this.uploadList.length === this.number) {
+ this.fileList = this.fileList.concat(this.uploadList);
+ this.uploadList = [];
+ this.number = 0;
+ this.$emit("input", this.listToString(this.fileList));
+ this.loading.close();
+ }
},
// 涓婁紶鍓峫oading鍔犺浇
handleBeforeUpload(file) {
@@ -163,6 +171,7 @@
text: "涓婁紶涓�",
background: "rgba(0, 0, 0, 0.7)",
});
+ this.number++;
},
// 鏂囦欢涓暟瓒呭嚭
handleExceed() {
--
Gitblit v1.9.3