Commit c8f3eb6fdfddbbc93e05ccd6f122f09d00566b82
1 parent
76034472
【修复】图片上传组件
Showing
3 changed files
with
30 additions
and
8 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.controller.admin.file; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.date.DateUtil; | |
| 3 | +import cn.hutool.core.io.FileUtil; | |
| 4 | 4 | import cn.hutool.core.io.IoUtil; |
| 5 | +import cn.hutool.core.util.IdUtil; | |
| 5 | 6 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | 8 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 8 | 9 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
| 9 | 10 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; |
| 11 | +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.SimpleUploadRespVO; | |
| 10 | 12 | import cn.iocoder.yudao.module.infra.convert.file.FileConvert; |
| 11 | 13 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; |
| 12 | 14 | import cn.iocoder.yudao.module.infra.service.file.FileService; |
| ... | ... | @@ -24,7 +26,6 @@ import org.springframework.web.multipart.MultipartFile; |
| 24 | 26 | import javax.annotation.Resource; |
| 25 | 27 | import javax.servlet.http.HttpServletResponse; |
| 26 | 28 | import javax.validation.Valid; |
| 27 | -import java.util.Date; | |
| 28 | 29 | |
| 29 | 30 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 30 | 31 | |
| ... | ... | @@ -54,10 +55,14 @@ public class FileController { |
| 54 | 55 | @ApiImplicitParams({ |
| 55 | 56 | @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), |
| 56 | 57 | }) |
| 57 | - public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception { | |
| 58 | - String path = DateUtil.format(new Date(), "yyyy/MM/dd/") + file.getOriginalFilename(); | |
| 59 | - String file1 = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); | |
| 60 | - return success(file1); | |
| 58 | + public CommonResult<SimpleUploadRespVO> uploadFile(@RequestParam("file") MultipartFile file) throws Exception { | |
| 59 | + SimpleUploadRespVO simpleUploadRespVO = new SimpleUploadRespVO(); | |
| 60 | + simpleUploadRespVO.setFileName(file.getOriginalFilename()); | |
| 61 | + // TODO 日期路径, 随机文件名 | |
| 62 | + String path = IdUtil.fastSimpleUUID() + "." + FileUtil.extName(file.getOriginalFilename()); | |
| 63 | + String fileUrl = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); | |
| 64 | + simpleUploadRespVO.setFileUrl(fileUrl); | |
| 65 | + return success(simpleUploadRespVO); | |
| 61 | 66 | } |
| 62 | 67 | |
| 63 | 68 | @DeleteMapping("/delete") | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.infra.controller.admin.file.vo.file; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModel; | |
| 4 | +import io.swagger.annotations.ApiModelProperty; | |
| 5 | +import lombok.Data; | |
| 6 | + | |
| 7 | +@Data | |
| 8 | +@ApiModel(value = "管理后台 - 简单上传文件 VO", description = "简单上传文件, 不需要 path") | |
| 9 | +public class SimpleUploadRespVO { | |
| 10 | + | |
| 11 | + @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") | |
| 12 | + private String fileName; | |
| 13 | + | |
| 14 | + @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg") | |
| 15 | + private String fileUrl; | |
| 16 | +} | ... | ... |
yudao-ui-admin/src/components/ImageUpload/index.vue
| ... | ... | @@ -93,7 +93,8 @@ export default { |
| 93 | 93 | // 然后将数组转为对象数组 |
| 94 | 94 | this.fileList = list.map(item => { |
| 95 | 95 | if (typeof item === "string") { |
| 96 | - if (item.indexOf(this.baseUrl) === -1) { | |
| 96 | + // 不带有 http 的路径, 才拼接 baseUrl. | |
| 97 | + if (item.indexOf("http") === -1) { | |
| 97 | 98 | item = { name: this.baseUrl + item, url: this.baseUrl + item }; |
| 98 | 99 | } else { |
| 99 | 100 | item = { name: item, url: item }; |
| ... | ... | @@ -127,7 +128,7 @@ export default { |
| 127 | 128 | }, |
| 128 | 129 | // 上传成功回调 |
| 129 | 130 | handleUploadSuccess(res) { |
| 130 | - this.uploadList.push({ name: res.data, url: res.data }); | |
| 131 | + this.uploadList.push({ name: res.data.fileName, url: res.data.fileUrl }); | |
| 131 | 132 | if (this.uploadList.length === this.number) { |
| 132 | 133 | this.fileList = this.fileList.concat(this.uploadList); |
| 133 | 134 | this.uploadList = []; | ... | ... |