【若依】element-ui、SpringBoot 图片上传

效果展示:

 前端代码展示

  • action:后端api,/dev-api 的作用是一个标识,用来给webpack重写成相对应的地址。
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  <el-form ref="form" :model="form" :rules="rules" label-width="100px">
    <el-form-item label="证明材料" prop="mainImage">
      <el-upload
        class="avatar-uploader"
        action="/dev-api/clock-in/qingjia/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
  </el-form>
</el-dialog>

data数据展示

open: false,
imageUrl: '',
rules: {
  userName: [
    { required: true, message: "角色名称不能为空", trigger: "blur" },
  ],
},

methods展示

handleAvatarSuccess(res, file) {
  this.imageUrl = URL.createObjectURL(file.raw);
  this.form.qjImage = file.response.fileName;
},
beforeAvatarUpload(file) {
  const isLt5M = file.size / 1024 / 1024 < 5;
  if (!isLt5M) {
    this.$message.error('上传头像图片大小不能超过 5MB!');
  }
  return isLt5M;
},

后端代码:(若依自带的代码,源码在若依common包下)

@RestController
@RequestMapping("/clock-in/qingjia")
public class HJZT_QingjiaController extends BaseController {

	@Autowired
	private ServerConfig serverConfig;
	
	@Anonymous
	@PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = HjZtConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

好的,以下是一个简单的 Spring BootElement UI 实现图片上传的示例代码: 前端代码: <template> <div> <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="false" :file-list="fileList" list-type="picture-card" :on-remove="handleRemove" > <i class="el-icon-plus"></i> </el-upload> </div> </template> <script> export default { data() { return { fileList: [] }; }, methods: { handleSuccess(response, file, fileList) { console.log(response); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg'; const isPNG = file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG && !isPNG) { this.$message.error('上传图片只能是 JPG/PNG 格式!'); } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!'); } return isJPG || isPNG && isLt2M; }, handleRemove(file, fileList) { console.log(file, fileList); } } }; </script> 后端代码: @RestController public class FileUploadController { @PostMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "上传失败,请选择文件"; } String fileName = file.getOriginalFilename(); String filePath = "D:/upload/"; File dest = new File(filePath + fileName); try { file.transferTo(dest); return "上传成功"; } catch (IOException e) { e.printStackTrace(); } return "上传失败!"; } } 这个示例中,前端使用了 Element UIUpload 组件,后端使用了 Spring Boot 的文件上传功能。在上传之前,前端会对图片进行格式和大小的校验,如果不符合要求会提示错误信息。后端会将上传的图片保存到指定的文件夹中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值