springboot Vue+elmentui 实现图片上传 可携带其他参数
2020-03-31 07:40
651 查看
首先先实现后端
controller类
@PostMapping("/addHome") public ResponseBase uploadWork(MultipartFile file, String homeAddress) { ResponseBase jsonData=new ResponseBase(); if (!file.isEmpty()) { try { if (file == null) { return "请选择要上传的图片"; } if (file.getSize() > 1024 * 1024 * 10) { return "文件大小不能大于10M"; } //获取文件后缀 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length()); if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) { jsonData.setMessage("请选择jpg,jpeg,gif,png格式的图片"); } String savePath = "D:\\image"; File savePathFile = new File(savePath); if (!savePathFile.exists()) { //若不存在该目录,则创建目录 savePathFile.mkdir(); System.out.println("我是不存在得!!!!!"); } //通过UUID生成唯一文件名 String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix; try { //将文件保存指定目录 file.transferTo(new File(savePath + "\\"+filename)); } catch (Exception e) { e.printStackTrace(); return "保存文件异常"; } //添加至数据库 } catch (Exception e) { e.printStackTrace(); } } return "ok"; }
前端
<el-form label-width="60" :model="mergeForm" :rules="ruleMergeForm" ref="mergeForm"> <el-upload ref="upload" action="http://localhost:8080/test/home/addHome" //你请求的地址 :limit="1"//限制的文件个数 :on-exceed="onExceed" :on-success="handleAvatarSuccess"//上传成功后 :data="mergeForm"//你要上传的其他参数 list-type="picture-card" :auto-upload="false">//取消自动上传 <i slot="default" class="el-icon-plus"></i> <div slot="file" slot-scope="{file}"> <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" > <span class="el-upload-list__item-actions"> <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)" > <i class="el-icon-zoom-in"></i> </span> </span> </div> </el-upload> <el-dialog :visible.sync="imgDialogVisible"> <img width="100%" :src="imageUrl" alt=""> </el-dialog> <el-form-item label="地址" prop="homeAddress"> <el-input v-model="mergeForm.homeAddress" autocomplete="off"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="doSubmitMergeForm('mergeForm')">确 定</el-button> </div> <script> export default { name: 'Home', data: function () { return { imageUrl: '', URL: "http://localhost:8080/ipfsemc/image/", result: [], imgDialogVisible: false, dialogFormVisible: false, dialogVisible: false, mergeForm: { homeId:"", homeAddress: "" }, ruleMergeForm: { homeAddress: [{ required: true, message: '跳转不能为空,且必须以http或https开头', trigger: 'blur' } ] } }; }, methods: { handleAvatarSuccess(res, file) { this.$message({ message: "成功!", type: 'success' }); }, handlePictureCardPreview(file) { this.imageUrl = file.url; this.imgDialogVisible = true; }, //上传的文件个数超出设定时触发的函数 onExceed(files, fileList) { this.$message({ type: 'info', message: '最多只能上传一个图片', duration: 6000 }); }, doSubmitMergeForm() { this.$refs['mergeForm'].validate((valid) => { //表单验证不通过则不可以提交 if (false === valid) { return false; } this.$refs.upload.submit()//图片上传 }); }, } } </script>
那里有问题 欢迎前来提问 你再成长 我也在成长!!! 要不你再点个赞支持一下不!!哈哈
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- springboot+vue+element ui(upload+download)实现excel上传及下载(附带通过网关、前端获取token封装和携带请求方式)
- sau交流学习社区--在element-ui中新建FormData对象组合上传图片和文件的文件对象,同时需要携带其他参数
- vue + element-ui + springboot + 阿里云OSS 使用表单进行图片文件等的上传(具有上传滚动条效果)
- 基于SpringBoot实现图片上传与显示
- JavaWeb——使用Vue+Spring Boot实现Excel上传
- vue+elementui el-upload上传文件携带参数
- Springboot实现图片、视频等各类文件上传
- 基于Spring Boot实现图片上传/加水印一把梭操作
- spring boot实现上传图片并在页面上显示
- spring boot 用js实现上传文件(包含其他字段)显示进度
- Spring Boot + Vue Element实现Excel文件上传、解析、下载(含完整实现过程)
- spring mvc实现文件上传并携带其他参数的示例
- spring boot实现图片的上传和下载功能
- SpringBoot+Editormd实现markdown文本编辑和图片上传
- 前台基于vue 图片上传 后台基于springboot的 图片压缩上传
- SpringBoot+Vue.js实现前后端分离的文件上传功能
- vue-cli + ElementUI 实现自定义接口上传图片
- springboot+editor实现图片上传
- 使用Vue+Spring Boot实现Excel上传功能
- Spring Boot实现上传图片