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

This commit is contained in:
YunaiV 2022-05-11 01:20:07 +08:00
parent f46d81dab5
commit 63e632ceb7
3 changed files with 20 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO;
import cn.iocoder.yudao.module.infra.convert.file.FileConvert; import cn.iocoder.yudao.module.infra.convert.file.FileConvert;
@ -42,8 +43,9 @@ public class FileController {
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
@ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
}) })
@OperateLog(logArgs = false) // 上传文件没有记录操作日志的必要
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("path") String path) throws Exception { @RequestParam(value = "path", required = false) String path) throws Exception {
return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
} }

View File

@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.infra.service.file;
import cn.hutool.core.io.FileTypeUtil; import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.file.core.client.FileClient; import cn.iocoder.yudao.framework.file.core.client.FileClient;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
@ -36,6 +38,12 @@ public class FileServiceImpl implements FileService {
@Override @Override
public String createFile(String path, byte[] content) throws Exception { public String createFile(String path, byte[] content) throws Exception {
// 计算默认的 path
String type = FileTypeUtil.getType(new ByteArrayInputStream(content));
if (StrUtil.isEmpty(path)) {
path = DigestUtil.md5Hex(content) + '.' + type;
}
// 上传到文件存储器 // 上传到文件存储器
FileClient client = fileConfigService.getMasterFileClient(); FileClient client = fileConfigService.getMasterFileClient();
Assert.notNull(client, "客户端(master) 不能为空"); Assert.notNull(client, "客户端(master) 不能为空");
@ -46,7 +54,7 @@ public class FileServiceImpl implements FileService {
file.setConfigId(client.getId()); file.setConfigId(client.getId());
file.setPath(path); file.setPath(path);
file.setUrl(url); file.setUrl(url);
file.setType(FileTypeUtil.getType(new ByteArrayInputStream(content))); file.setType(type);
file.setSize(content.length); file.setSize(content.length);
fileMapper.insert(file); fileMapper.insert(file);
return url; return url;

View File

@ -2,7 +2,7 @@
<div class="component-upload-image"> <div class="component-upload-image">
<el-upload <el-upload
multiple multiple
:action="uploadImgUrl" :action="url"
list-type="picture-card" list-type="picture-card"
:on-success="handleUploadSuccess" :on-success="handleUploadSuccess"
:before-upload="handleBeforeUpload" :before-upload="handleBeforeUpload"
@ -76,11 +76,8 @@ export default {
dialogImageUrl: "", dialogImageUrl: "",
dialogVisible: false, dialogVisible: false,
hideUpload: false, hideUpload: false,
baseUrl: process.env.VUE_APP_BASE_API, url: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", //
uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // headers: { Authorization: "Bearer " + getAccessToken() }, //
headers: {
Authorization: "Bearer " + getAccessToken(),
},
fileList: [] fileList: []
}; };
}, },
@ -93,11 +90,8 @@ export default {
// //
this.fileList = list.map(item => { this.fileList = list.map(item => {
if (typeof item === "string") { if (typeof item === "string") {
if (item.indexOf(this.baseUrl) === -1) { // edit by
item = { name: this.baseUrl + item, url: this.baseUrl + item }; item = { name: item, url: item };
} else {
item = { name: item, url: item };
}
} }
return item; return item;
}); });
@ -127,7 +121,8 @@ export default {
}, },
// //
handleUploadSuccess(res) { handleUploadSuccess(res) {
this.uploadList.push({ name: res.fileName, url: res.fileName }); // edit by
this.uploadList.push({ name: res.data, url: res.data });
if (this.uploadList.length === this.number) { if (this.uploadList.length === this.number) {
this.fileList = this.fileList.concat(this.uploadList); this.fileList = this.fileList.concat(this.uploadList);
this.uploadList = []; this.uploadList = [];
@ -188,7 +183,7 @@ export default {
for (let i in list) { for (let i in list) {
strs += list[i].url.replace(this.baseUrl, "") + separator; strs += list[i].url.replace(this.baseUrl, "") + separator;
} }
return strs != '' ? strs.substr(0, strs.length - 1) : ''; return strs !== '' ? strs.substr(0, strs.length - 1) : '';
} }
} }
}; };