From c8f3eb6fdfddbbc93e05ccd6f122f09d00566b82 Mon Sep 17 00:00:00 2001 From: jeromesoar Date: Sun, 24 Apr 2022 22:06:22 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=B8=8A=E4=BC=A0=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/file/FileController.java | 17 +++++++++++------ .../admin/file/vo/file/SimpleUploadRespVO.java | 16 ++++++++++++++++ .../src/components/ImageUpload/index.vue | 5 +++-- 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index f53b45ac8..071c89f43 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java @@ -1,12 +1,14 @@ package cn.iocoder.yudao.module.infra.controller.admin.file; -import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.IdUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; 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.SimpleUploadRespVO; import cn.iocoder.yudao.module.infra.convert.file.FileConvert; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.service.file.FileService; @@ -24,7 +26,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; -import java.util.Date; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -54,10 +55,14 @@ public class FileController { @ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), }) - public CommonResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception { - String path = DateUtil.format(new Date(), "yyyy/MM/dd/") + file.getOriginalFilename(); - String file1 = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); - return success(file1); + public CommonResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception { + SimpleUploadRespVO simpleUploadRespVO = new SimpleUploadRespVO(); + simpleUploadRespVO.setFileName(file.getOriginalFilename()); + // TODO 日期路径, 随机文件名 + String path = IdUtil.fastSimpleUUID() + "." + FileUtil.extName(file.getOriginalFilename()); + String fileUrl = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); + simpleUploadRespVO.setFileUrl(fileUrl); + return success(simpleUploadRespVO); } @DeleteMapping("/delete") diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java new file mode 100644 index 000000000..369d9f578 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java @@ -0,0 +1,16 @@ +package cn.iocoder.yudao.module.infra.controller.admin.file.vo.file; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +@ApiModel(value = "管理后台 - 简单上传文件 VO", description = "简单上传文件, 不需要 path") +public class SimpleUploadRespVO { + + @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") + private String fileName; + + @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg") + private String fileUrl; +} diff --git a/yudao-ui-admin/src/components/ImageUpload/index.vue b/yudao-ui-admin/src/components/ImageUpload/index.vue index b736b7240..881cdb07d 100644 --- a/yudao-ui-admin/src/components/ImageUpload/index.vue +++ b/yudao-ui-admin/src/components/ImageUpload/index.vue @@ -93,7 +93,8 @@ export default { // 然后将数组转为对象数组 this.fileList = list.map(item => { if (typeof item === "string") { - if (item.indexOf(this.baseUrl) === -1) { + // 不带有 http 的路径, 才拼接 baseUrl. + if (item.indexOf("http") === -1) { item = { name: this.baseUrl + item, url: this.baseUrl + item }; } else { item = { name: item, url: item }; @@ -127,7 +128,7 @@ export default { }, // 上传成功回调 handleUploadSuccess(res) { - this.uploadList.push({ name: res.data, url: res.data }); + this.uploadList.push({ name: res.data.fileName, url: res.data.fileUrl }); if (this.uploadList.length === this.number) { this.fileList = this.fileList.concat(this.uploadList); this.uploadList = [];