您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)使用upload組件怎么實(shí)現(xiàn)多圖片上傳,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1.我們要利用element-ui的Upload組件布置界面:
//upload.vue <el-upload :action= domain ref="upload" accept='image/jpeg,image/gif,image/png' :auto-upload="false" :http-request="upqiniu" :limit="limit" :multiple="multiple" list-type="picture-card" :before-upload="beforeUpload" :on-preview="handlePictureCardPreview" :on-change="handldChange" :on-remove="handleRemove"> <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt=""> </el-dialog>
domain
指的是我們的上傳地址,upqiniu
是我們自定義的上傳方法,beforeUpload
是圖片上傳前執(zhí)行的方法。關(guān)于該組件的其他用法可以在element的官方文檔查閱:Upload 上傳
2.對圖片進(jìn)行壓縮
// upload.vue imgQuality: 0.5, //壓縮圖片的質(zhì)量 dataURItoBlob(dataURI, type) { var binary = atob(dataURI.split(',')[1]); var array = []; for(var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); } return new Blob([new Uint8Array(array)], {type: type}); }, beforeUpload(param) { //對圖片進(jìn)行壓縮 const imgSize = param.size / 1024 / 1024 if(imgSize > 1) { const _this = this return new Promise(resolve => { const reader = new FileReader() const image = new Image() image.onload = (imageEvent) => { const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); const width = image.width * _this.imgQuality const height = image.height * _this.imgQuality canvas.width = width; canvas.height = height; context.clearRect(0, 0, width, height); context.drawImage(image, 0, 0, width, height); const dataUrl = canvas.toDataURL(param.type); const blobData = _this.dataURItoBlob(dataUrl, param.type); resolve(blobData) } reader.onload = (e => { image.src = e.target.result; }); reader.readAsDataURL(param); }) } }
壓縮圖片實(shí)現(xiàn)起來比較簡單。就是在beforeUpload()方法里面return一個Promise,Promise里面我們把圖片的長度和寬度按比例進(jìn)行縮小,并把圖片畫到canvas上,然后把canvas轉(zhuǎn)成一個blod對象。
3.前端向后端請求上傳token。
//upload.vue upqiniu(param) { let filetype = '' if (param.file.type === 'image/png') { filetype = 'png' } else { filetype = 'jpg' } const formdata = { filetype: filetype, param: param } this.actionGetUploadToken(formdata) } // vuex/action.js actionGetUploadToken({commit}, obj) { const msg = { filetype: obj.filetype } usersApi.getImgUploadToken(msg).then((response) => { if(response.stateCode === 200) { commit('uploadImg', {'token': response.token, 'key': response.key, 'param': obj.param}) } }, (error) => { console.log(`獲取圖片上傳憑證錯誤:${error}`) commit('uploadImgError') }) },
4.后端生成上傳token,并發(fā)給前端,我用Python實(shí)現(xiàn)。
filetype = data.get('filetype') # 構(gòu)建鑒權(quán)對象 q = Auth(configs.get('qiniu').get('AK'), configs.get('qiniu').get('SK')) # 生成圖片名 salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) key = salt + '_' + str(int(time.time())) + '.' + filetype # 生成上傳 Token,可以指定過期時間等 token = q.upload_token(configs.get('qiniu').get('bucket_name'), key, 3600) return Response({"stateCode": 200, "token": token, "key": key}, 200)
5.前端接收token,開始向服務(wù)器上傳圖片
// vuex/state.js imgName: [], //圖片名數(shù)組 // vuex/mutations.js uploadImg(state, msg) { const config = { useCdnDomain: true, region: qiniu.region.z2 } var putExtra = { fname: msg.param.file.name, params: {}, mimeType: ["image/png", "image/jpeg", "image/gif"] }; var observer = { next(res){ }, error(err){ console.log(`圖片上傳錯誤信息:${err.message}`) }, complete(res){ console.log(`圖片上傳成功:${res.key}`) state.imgName.push(res.key) } } var observable = qiniu.upload(msg.param.file, msg.key, msg.token, putExtra, config) //上傳開始 var subscription = observable.subscribe(observer) },
6.上傳成功以后,將圖片名存入數(shù)據(jù)庫
// 用到upload.vue的界面 this.imgsList = this.imgName.map(key => `http://${this.qiniuaddr}/${key}`) switch(this.imgsList.length) { case 4: this.img4 = this.imgsList[3] case 3: this.img3 = this.imgsList[2] case 2: this.img2 = this.imgsList[1] case 1: this.img1 = this.imgsList[0] } let obj = { goods_img1: this.img1, goods_img2:this.img2, goods_img3:this.img3, goods_img4:this.img4 } //將信息發(fā)送給后端 this.actionPublish(obj)
看完上述內(nèi)容,你們對使用upload組件怎么實(shí)現(xiàn)多圖片上傳有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。