Commit 63e632ceb7c9df92b406017b16039a83ee3e9218

Authored by YunaiV
1 parent f46d81da

图片上传组件 ImageUpload 上传报错的问题

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java
@@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil; @@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil;
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 4 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
5 import cn.iocoder.yudao.framework.common.pojo.PageResult; 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; 6 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
  7 +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
7 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; 8 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
8 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; 9 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO;
9 import cn.iocoder.yudao.module.infra.convert.file.FileConvert; 10 import cn.iocoder.yudao.module.infra.convert.file.FileConvert;
@@ -42,8 +43,9 @@ public class FileController { @@ -42,8 +43,9 @@ public class FileController {
42 @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), 43 @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
43 @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) 44 @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
44 }) 45 })
  46 + @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
45 public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, 47 public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
46 - @RequestParam("path") String path) throws Exception { 48 + @RequestParam(value = "path", required = false) String path) throws Exception {
47 return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); 49 return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
48 } 50 }
49 51
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
@@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.infra.service.file; @@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.infra.service.file;
2 2
3 import cn.hutool.core.io.FileTypeUtil; 3 import cn.hutool.core.io.FileTypeUtil;
4 import cn.hutool.core.lang.Assert; 4 import cn.hutool.core.lang.Assert;
  5 +import cn.hutool.core.util.StrUtil;
  6 +import cn.hutool.crypto.digest.DigestUtil;
5 import cn.iocoder.yudao.framework.common.pojo.PageResult; 7 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 import cn.iocoder.yudao.framework.file.core.client.FileClient; 8 import cn.iocoder.yudao.framework.file.core.client.FileClient;
7 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; 9 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
@@ -36,6 +38,12 @@ public class FileServiceImpl implements FileService { @@ -36,6 +38,12 @@ public class FileServiceImpl implements FileService {
36 38
37 @Override 39 @Override
38 public String createFile(String path, byte[] content) throws Exception { 40 public String createFile(String path, byte[] content) throws Exception {
  41 + // 计算默认的 path 名
  42 + String type = FileTypeUtil.getType(new ByteArrayInputStream(content));
  43 + if (StrUtil.isEmpty(path)) {
  44 + path = DigestUtil.md5Hex(content) + '.' + type;
  45 + }
  46 +
39 // 上传到文件存储器 47 // 上传到文件存储器
40 FileClient client = fileConfigService.getMasterFileClient(); 48 FileClient client = fileConfigService.getMasterFileClient();
41 Assert.notNull(client, "客户端(master) 不能为空"); 49 Assert.notNull(client, "客户端(master) 不能为空");
@@ -46,7 +54,7 @@ public class FileServiceImpl implements FileService { @@ -46,7 +54,7 @@ public class FileServiceImpl implements FileService {
46 file.setConfigId(client.getId()); 54 file.setConfigId(client.getId());
47 file.setPath(path); 55 file.setPath(path);
48 file.setUrl(url); 56 file.setUrl(url);
49 - file.setType(FileTypeUtil.getType(new ByteArrayInputStream(content))); 57 + file.setType(type);
50 file.setSize(content.length); 58 file.setSize(content.length);
51 fileMapper.insert(file); 59 fileMapper.insert(file);
52 return url; 60 return url;
yudao-ui-admin/src/components/ImageUpload/index.vue
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <div class="component-upload-image"> 2 <div class="component-upload-image">
3 <el-upload 3 <el-upload
4 multiple 4 multiple
5 - :action="uploadImgUrl" 5 + :action="url"
6 list-type="picture-card" 6 list-type="picture-card"
7 :on-success="handleUploadSuccess" 7 :on-success="handleUploadSuccess"
8 :before-upload="handleBeforeUpload" 8 :before-upload="handleBeforeUpload"
@@ -76,11 +76,8 @@ export default { @@ -76,11 +76,8 @@ export default {
76 dialogImageUrl: "", 76 dialogImageUrl: "",
77 dialogVisible: false, 77 dialogVisible: false,
78 hideUpload: false, 78 hideUpload: false,
79 - baseUrl: process.env.VUE_APP_BASE_API,  
80 - uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址  
81 - headers: {  
82 - Authorization: "Bearer " + getAccessToken(),  
83 - }, 79 + url: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
  80 + headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部
84 fileList: [] 81 fileList: []
85 }; 82 };
86 }, 83 },
@@ -93,11 +90,8 @@ export default { @@ -93,11 +90,8 @@ export default {
93 // 然后将数组转为对象数组 90 // 然后将数组转为对象数组
94 this.fileList = list.map(item => { 91 this.fileList = list.map(item => {
95 if (typeof item === "string") { 92 if (typeof item === "string") {
96 - if (item.indexOf(this.baseUrl) === -1) {  
97 - item = { name: this.baseUrl + item, url: this.baseUrl + item };  
98 - } else {  
99 - item = { name: item, url: item };  
100 - } 93 + // edit by 芋道源码
  94 + item = { name: item, url: item };
101 } 95 }
102 return item; 96 return item;
103 }); 97 });
@@ -127,7 +121,8 @@ export default { @@ -127,7 +121,8 @@ export default {
127 }, 121 },
128 // 上传成功回调 122 // 上传成功回调
129 handleUploadSuccess(res) { 123 handleUploadSuccess(res) {
130 - this.uploadList.push({ name: res.fileName, url: res.fileName }); 124 + // edit by 芋道源码
  125 + this.uploadList.push({ name: res.data, url: res.data });
131 if (this.uploadList.length === this.number) { 126 if (this.uploadList.length === this.number) {
132 this.fileList = this.fileList.concat(this.uploadList); 127 this.fileList = this.fileList.concat(this.uploadList);
133 this.uploadList = []; 128 this.uploadList = [];
@@ -188,7 +183,7 @@ export default { @@ -188,7 +183,7 @@ export default {
188 for (let i in list) { 183 for (let i in list) {
189 strs += list[i].url.replace(this.baseUrl, "") + separator; 184 strs += list[i].url.replace(this.baseUrl, "") + separator;
190 } 185 }
191 - return strs != '' ? strs.substr(0, strs.length - 1) : ''; 186 + return strs !== '' ? strs.substr(0, strs.length - 1) : '';
192 } 187 }
193 } 188 }
194 }; 189 };