vue-cli + ElementUI 实现自定义接口上传图片
2018-12-13 15:37
1276 查看
1、使用ElementUI风装好的方法
<el-upload class="upload-demo" :before-remove="beforeRemove" //移除前动作 :on-remove="handleRemove" //移除时动作 :action="action" //上传接口 :show-file-list="true" //是否显示文件列表 :http-request='httprequest' //拦截上传动作 :before-upload="beforeupload" //选择完图片钩子 :file-list="fileList" //展示图片列表data list-type="picture-card" multiple> <el-button size="small" type="primary">点击上传</el-button> </el-upload>
参数解释再ElementUI官网
2、在data里定义需要用到的参数(可能有遗漏)
imageUrl:'', action:'', //上传接口 例子中接口需要封装formdata上传 fileList: [], imgname:'', delfileuid:'',
3、在methods中定义方法
//点击移除图片时 beforeRemove(file, fileList) { this.delfileuid = file.uid; console.log(file.uid); return this.$confirm(`确定移除 ${ file.name }?`); }, //移除图片动作 handleRemove(file, fileList) { var that=this; console.log(that.delfileuid) for(let i=0;i<that.fileList.length;i++) { if(that.fileList[i].uid == that.delfileuid){ that.fileList.splice(i,1) console.log(that.fileList) } } }, //阻止upload的自己上传,进行再操作 beforeupload(file) { this.imgname = file.name; const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!'); } else{ ////////console.log(file); //创建临时的路径来展示图片 // var windowURL = window.URL || window.webkitURL; // this.imageUrl=windowURL.createObjectURL(file); this.param = new FormData(); this.param.append('imgFile', file, file.imgFile); var names = this.form.imgFile; //下面append的东西就会到form表单数据的fields中; this.param.append('message', names); } return isLt2M; }, httprequest() { var that=this; let config = { headers: { 'Content-Type': 'multipart/form-data' } }; this.$axios.post(this.action, this.param, config).then(function(res) { if(res.data.invokeResultCode == 200){ //本项目中判断接口返回code that.$message.success("上传成功"); that.imageUrl=res.data.data.url; that.fileList.push({name: that.imgname, url:res.data.data.url }) } else{ this.$message.error(res.data.invokeResultMessage); } }).catch((res) => { this.$message.error('请稍候重试或联系管理员'); }) },
相关文章推荐
- vue-cli结合Element-ui基于cropper.js封装vue实现图片裁剪组件功能
- vue2.0 使用element-ui里的upload组件实现图片预览效果
- elementUI+koa实现图片上传功能
- Element UI - http-request 覆盖默认的上传行为,自定义上传的实现
- vue+elementui的table行内实现el-upload文件添加/多文件上传
- Vue + Element UI 实现权限管理系统 前端篇(九):接口格式定义
- vue2.0 使用element-ui里的upload组件实现多图上传。采用FORMDATA的方式上传。
- Vue上传文件:ElementUI中的upload实现
- vue2.0 使用element-ui里的upload组件实现图片预览效果方法
- 关于vue+element ui 实现上传文件
- android自定义View实现图片上传进度显示(仿手机QQ上传效果)
- vue中Element-ui 输入银行账号每四位加一个空格的实现代码
- 使用Vue实现图片上传的三种方式
- javascript实现拼图游戏(支持自定义上传图片,后台自动分割)
- [置顶] 用Vue、element-ui、axios实现省市区三级联动
- Vue + Element UI 实现权限管理系统 前端篇(十五):嵌套外部网页
- vue2.0 自定义 图片上传(UpLoader)组件
- sau交流学习社区--在element-ui中新建FormData对象组合上传图片和文件的文件对象,同时需要携带其他参数
- 【原创】.Net 微信 JS-SDK图片、语音上传接口的实现(MVC)-(二 、上传图片服务器操作)
- vue实现类似淘宝商品评价页面星级评价及上传多张图片功能