fix(前端 ImageUpload 组件):
- 前端 ImageUpload 组件, 上传路径以及地址拼接 - 基础设施-文件上传, 新增简单上传接口pull/2/head
parent
76d310610b
commit
8391898b26
|
@ -1,11 +1,14 @@
|
|||
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.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;
|
||||
|
@ -47,6 +50,21 @@ public class FileController {
|
|||
return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@ApiOperation("删除文件")
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -43,7 +43,7 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
import { getToken } from "@/utils/auth";
|
||||
import {getToken} from "@/utils/auth";
|
||||
|
||||
export default {
|
||||
props: {
|
||||
|
@ -77,7 +77,7 @@ export default {
|
|||
dialogVisible: false,
|
||||
hideUpload: false,
|
||||
baseUrl: process.env.VUE_APP_BASE_API,
|
||||
uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
|
||||
uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/simple-upload", // 上传的图片服务器地址
|
||||
headers: {
|
||||
Authorization: "Bearer " + getToken(),
|
||||
},
|
||||
|
@ -93,10 +93,11 @@ export default {
|
|||
// 然后将数组转为对象数组
|
||||
this.fileList = list.map(item => {
|
||||
if (typeof item === "string") {
|
||||
if (item.indexOf(this.baseUrl) === -1) {
|
||||
item = { name: this.baseUrl + item, url: this.baseUrl + item };
|
||||
// 不带有 http 的路径, 才拼接 baseUrl.
|
||||
if (item.indexOf("http") === -1) {
|
||||
item = {name: this.baseUrl + item, url: this.baseUrl + item};
|
||||
} else {
|
||||
item = { name: item, url: item };
|
||||
item = {name: item, url: item};
|
||||
}
|
||||
}
|
||||
return item;
|
||||
|
@ -127,7 +128,7 @@ export default {
|
|||
},
|
||||
// 上传成功回调
|
||||
handleUploadSuccess(res) {
|
||||
this.uploadList.push({ name: res.fileName, url: res.fileName });
|
||||
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 = [];
|
||||
|
|
Loading…
Reference in New Issue