mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 10:37:41 +08:00 
			
		
		
		
	- 文件上传接口合并
- ImageUpload 组件回调 url 处理
This commit is contained in:
		| @ -3,12 +3,13 @@ package cn.iocoder.yudao.module.infra.controller.admin.file; | ||||
| import cn.hutool.core.io.FileUtil; | ||||
| import cn.hutool.core.io.IoUtil; | ||||
| import cn.hutool.core.util.IdUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| 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.controller.admin.file.vo.file.UploadRespVO; | ||||
| 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; | ||||
| @ -45,24 +46,20 @@ public class FileController { | ||||
|             @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), | ||||
|             @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) | ||||
|     }) | ||||
|     public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, | ||||
|                                            @RequestParam("path") String path) throws Exception { | ||||
|         return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); | ||||
|     public CommonResult<UploadRespVO> uploadFile(@RequestParam("file") MultipartFile file, | ||||
|                                                  @RequestParam(value = "path", required = false) String path) | ||||
|             throws Exception { | ||||
|         // 如果路径没传, 系统生成随机路径 | ||||
|         if (StrUtil.isBlank(path)) { | ||||
|             // TODO 生成带日期的路径, 目前 #getFileContent 不支持 | ||||
|             path = IdUtil.fastSimpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); | ||||
|         } | ||||
|  | ||||
|     @PostMapping("/simple-upload") | ||||
|     @ApiOperation("简单上传文件") | ||||
|     @ApiImplicitParams({ | ||||
|             @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), | ||||
|     }) | ||||
|     public CommonResult<SimpleUploadRespVO> 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); | ||||
|         // 返回结果 | ||||
|         UploadRespVO uploadRespVO = new UploadRespVO(); | ||||
|         uploadRespVO.setFileName(file.getOriginalFilename()); | ||||
|         uploadRespVO.setFileUrl(fileUrl); | ||||
|         return success(uploadRespVO); | ||||
|     } | ||||
|  | ||||
|     @DeleteMapping("/delete") | ||||
|  | ||||
| @ -5,8 +5,8 @@ import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.Data; | ||||
| 
 | ||||
| @Data | ||||
| @ApiModel(value = "管理后台 - 简单上传文件 VO", description = "简单上传文件, 不需要 path") | ||||
| public class SimpleUploadRespVO { | ||||
| @ApiModel(value = "管理后台 - 上传文件 VO") | ||||
| public class UploadRespVO { | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") | ||||
|     private String fileName; | ||||
| @ -76,8 +76,9 @@ export default { | ||||
|       dialogImageUrl: "", | ||||
|       dialogVisible: false, | ||||
|       hideUpload: false, | ||||
|       // todo /infra/file/upload 返回的都是带 host 的可访问地址, baseUrl 有点没必要 | ||||
|       baseUrl: process.env.VUE_APP_BASE_API, | ||||
|       uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/simple-upload", // 上传的图片服务器地址 | ||||
|       uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 上传的图片服务器地址 | ||||
|       headers: { | ||||
|         Authorization: "Bearer " + getToken(), | ||||
|       }, | ||||
| @ -187,7 +188,8 @@ export default { | ||||
|       let strs = ""; | ||||
|       separator = separator || ","; | ||||
|       for (let i in list) { | ||||
|         strs += list[i].url.replace(this.baseUrl, "") + separator; | ||||
|         // 由于 infra-file 返回带有 host , 不需要替换 baseUrl // .replace(this.baseUrl, "") | ||||
|         strs += list[i].url + separator; | ||||
|       } | ||||
|       return strs != '' ? strs.substr(0, strs.length - 1) : ''; | ||||
|     } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 JeromeSoar
					JeromeSoar