vue圖片上傳組件使用詳解
vue圖片上傳組件,供大家參考,具體內(nèi)容如下
最近在做項(xiàng)目的時(shí)候順便補(bǔ)充了一下公司項(xiàng)目的公共組件庫(kù),剛剛手頭事情告一段落,就來(lái)做個(gè)筆記。
首先來(lái)看看最終效果
1.不允許刪除
2.允許用戶刪除(顯示刪除按鈕)
實(shí)現(xiàn)的效果就是上圖顯示內(nèi)容
接下來(lái)說(shuō)說(shuō)組件布局那部分直接上代碼了
<template> <div class="uploadImg"> <div class="upload-content"> <div class="upload-title"> <p>{{upTitle}}</p> <p class="show-num">{{upNum}}/{{uploadNum}}</p> </div> <ul class="img-list"> <li class="show-img" v-for="(item, index) in imgList" :key="index"> <img :src="item" alt=""> <kk-icon :class="isDel == true ? '' : 'hide-del'" name="error" color="#FF685D" size="0.4rem" @click.native="delImg(index)"></kk-icon> </li> <div class="uploadSec"> <img :src="globalPath+'img/insurance/upload.png'" alt="上傳圖標(biāo)"> <input type="file" value="" id="choose" @change='onUpload' multiple> </div> </ul> </div> </div> </template>
上面代碼中的kk-icon是自定義的圖標(biāo)組件,只是多了個(gè)接受顏色和大小的功能,你們自己寫一個(gè)替換就行了。然后動(dòng)態(tài)定義它的class,實(shí)現(xiàn)顯隱就結(jié)束了。
隨手貼個(gè)樣式
<style lang="less" type="text/less" rel="stylesheet/less"> .uploadImg{ text-align: left; .upload-content{ margin-left: 0.3rem; .upload-title{ display: flex; justify-content: space-between; padding: 0.3rem 0.3rem 0.3rem 0; .show-num{ color: #c9c9c9; } } .img-list{ display: inline-block; margin: 0.6rem 0.3rem 0.3rem 0; .show-img{ position: relative; display: inline-block; margin-right: 0.12rem; height: 1.3rem; width: 1.3rem; img{ width: 100%; height: 100%; } .hide-del{ display: none; } .yd-icon-error{ position: absolute; top: 0; right: 0; } } .uploadSec{ position: relative; display: inline-block; width: 1.3rem; height: 1.3rem; overflow: hidden; img{ width: 100%; height: 100%; } #choose{ position: absolute; height: 100%; left: 0; top: 0; opacity: 0; } } } } } </style>
接下來(lái)看看實(shí)現(xiàn)邏輯
借助input type="file"實(shí)現(xiàn)圖片選擇,是否允許多選圖片的話是通過(guò)給input的multiple屬性。(其他直接備注在里面了)
在組件中接收父組件傳來(lái)的圖片數(shù)量限制,是否開(kāi)啟刪除操作,上傳圖片地址,預(yù)覽地址等
props: ['imgNum','title','upUrl','showUrl','showDel'],
title 上傳組件的標(biāo)題
imgNum 上傳圖片數(shù)量限制
upUrl 設(shè)置上傳圖片地址
showUrl設(shè)置圖片回顯地址
showDel是否允許刪除圖片
changeNum 圖片改變時(shí),觸發(fā)父組件中的方法
當(dāng)選擇圖片確定后就會(huì)觸發(fā)change,因此我在@change="onUpload" 進(jìn)行上傳,上傳使用了formData
// 上傳操作 onUpload (e) { let photoFile = e.target let val = e.target.value // 判斷是否符合上傳條件 if (photoFile.files.length === 0) return false for (let i = 0; i < photoFile.files.length; i++) { this.fileAdd(photoFile.files[i],i) } }
上傳操作中觸發(fā)圖片后續(xù)處理方法fileAdd
因?yàn)楹笈_(tái)要求拿到的圖片地址是一串字符串,所以我在下面中使用join() 進(jìn)行數(shù)組轉(zhuǎn)化處理(因?yàn)楹笈_(tái)不支持接受圖片數(shù)組,因此我這個(gè)上傳多選圖片之后上傳也是單張上傳)
// 添加圖片 fileAdd (file,index) { let csrf_token = this.getCookie('XSRF-TOKEN'); let formFile = new FormData(); let imgName = 'img0'; formFile.append(imgName, file); formFile.append("_token", csrf_token); let name = file.name let size = file.size let types = '.jpg,.jpeg,.png,.gif' //文件格式 let fileExt = name.substring(name.lastIndexOf('.')).toLowerCase() let result = types.indexOf(fileExt) if (result < 0) { //驗(yàn)證圖片格式 this.$dialog.toast({ mes: '圖片格式不正確', timeout: 1000 }) return false } if (size > 1 * 1024 * 1024) { this.$dialog.toast({ mes: '圖片大小不能大于1M', timeout: 1000 }) return false } if (this.fileList.length >= this.uploadNum) { this.$dialog.toast({ mes: '圖片最多只能上傳' + this.uploadNum + '張', timeout: 1000 }) return false } axios.post(this.upUrl,formFile) .then((res) => { this.upNum = this.fileList.length + 1; //計(jì)算圖片數(shù)量 this.fileList.push(file); //添加進(jìn)圖片數(shù)組 let imgUrl = this.showUrl + res.data.data; //圖片回顯地址 let upImg = res.data.data; //傳給后臺(tái)的圖片地址 this.imgList.push(imgUrl); this.upImgList.push(upImg); let upImgAll = this.upImgList.join(','); this.$emit('input', upImgAll); }).catch((err) => { console.log(err); }) },
刪除圖片操作
我這刪除僅僅是對(duì)最后提交的圖片數(shù)組進(jìn)行處理,并未對(duì)上傳到圖片服務(wù)器上的圖片進(jìn)行移除處理
// 刪除圖片 delImg (index) { this.imgList.splice(index, 1); this.fileList.splice(index, 1); this.upNum = this.imgList.length; let imgAll = this.imgList.join(','); this.$emit('input', imgAll); },
最后我在組件中監(jiān)聽(tīng)了圖片改變
watch: { imgList () { this.$emit('changeNum'); //觸發(fā)父組件中驗(yàn)證資料是否完整的方法 } },
就是這樣了,一個(gè)簡(jiǎn)易的上傳組件(寫的不是很好,輕噴),還有可以優(yōu)化的地方,后面改完再看看吧
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue-cropper組件實(shí)現(xiàn)圖片切割上傳
- vue-cropper插件實(shí)現(xiàn)圖片截取上傳組件封裝
- vue 圖片裁剪上傳組件的實(shí)現(xiàn)
- vue3.0搭配.net core實(shí)現(xiàn)文件上傳組件
- vue 使用原生組件上傳圖片的實(shí)例
- Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能詳解
- vue中使用elementUI組件手動(dòng)上傳圖片功能
- Vue開(kāi)發(fā)之封裝上傳文件組件與用法示例
- vue圖片上傳本地預(yù)覽組件使用詳解
- vue-cli3.0+element-ui上傳組件el-upload的使用
- vue 實(shí)現(xiàn)上傳組件
相關(guān)文章
element-ui 上傳圖片后清空?qǐng)D片顯示的實(shí)例
今天小編就為大家分享一篇element-ui 上傳圖片后清空?qǐng)D片顯示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09使用elementUI table展開(kāi)行內(nèi)嵌套table問(wèn)題
這篇文章主要介紹了使用elementUI table展開(kāi)行內(nèi)嵌套table問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04