vue實(shí)現(xiàn)移動(dòng)端圖片上傳功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)移動(dòng)端圖片上傳的具體代碼,供大家參考,具體內(nèi)容如下
<template> <div class="box"> <div class="upDiv"> {{labTex}} //標(biāo)題 //上傳按鈕 <input ref="uploadInput" type="file" class='upinp' name="file" value="" accept="image/gif,image/jpeg,image/jpg,image/png" @change="selectImg($event)"/> </div> //顯示上傳圖片的區(qū)域 <div :class="operationShow?'operation-div':'operation-div hide'"> <img class="shoImg" :src="imgDefault"> </div> </div> </template> <script> export default { props: { value:{ type:String , default:'' }, labTex:{ type:String , default:'' }, imgDefault:{ type:String , default:'' } }, data() { return { dataUrl: '', defaultImg:'' } }, mounted() { console.log(this.value) console.log(this.labTex) }, // input的change回調(diào)第一個(gè)參數(shù)是event對(duì)象 methods: { selectImg(e){ const imgFile = e.target.files[0]; if (imgFile) { this.operationShow=true if(this.checkFile(imgFile)){ this.upload(imgFile); } } }, checkFile(file){ //文件為空判斷 if (file === null || file === undefined) { alert("請(qǐng)選擇您要上傳的文件!"); return false; }else{ return true; } let size = Math.floor(file.size / 1024); // 這個(gè)條件還得改 if (size!=0) { return true; }else{ return false } }, upload(file){ try { let self = this; var result=''; //執(zhí)行上傳操作 var xhr = new XMLHttpRequest(); xhr.open("post", ApiUrl+"/member/image/upload", true); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if (xhr.status == 200) { let returnData = $.parseJSON(xhr.responseText); if (!returnData) throw new Error("上傳失敗SERVER"); if (!returnData.code) throw new Error("上傳失敗SERVER") if (returnData.code == 200) { alert("上傳成功") //顯示圖片地址 self.$emit('change-img',returnData.name); self.defaultImg = returnData.url; } else { alert("上傳失敗SERVER") } console.log(""+returnDate) } else { alert("上傳失敗") } }; }; var token = getMemberToken(); //表單數(shù)據(jù) var fd = new FormData(); fd.append("token", token); fd.append("file", file); //執(zhí)行發(fā)送 result = xhr.send(fd); } catch (e) { console.log(e); alert(e); } } } } </script> <style> .box { height: 11rem; margin-top: 0.5rem; } .upDiv{ position:relative; height:1.2rem; width:100%; margin-bottom:0.08rem; width:5.5rem; text-align: center; z-index:10; font-size: 0.6rem; padding: 0 0.2rem; border-radius: 0.2rem; border-radius: 0.4rem; color: #fff; border: none; height: 1.2rem; line-height: 1.2rem; display: inline-block; background: #0097ffd9; } .operation-div{ width: 15rem; height: 9.2rem; } .operation-div img{ width:100%; height:100%; } .upDiv .upinp{ position:absolute; left:0px; right:0px; right:0px; bottom:0px; z-index:1; opacity:0; } .shoImg{ width:15rem; border-radius:0.05rem } </style>
在頁(yè)面當(dāng)中的使用:
<upload-img :lab-tex="`上傳銀行卡正面`" :img-default="imgFourDefault" v-on:change-img="chooseFourImg" ></upload-img>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:javascript實(shí)現(xiàn)點(diǎn)擊星星小游戲
欄 目:JavaScript
下一篇:webpack打包html里面img后src為“[object Module]”問題
本文標(biāo)題:vue實(shí)現(xiàn)移動(dòng)端圖片上傳功能
本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9417.html
您可能感興趣的文章
- 04-02java后端代碼分頁(yè) java后端實(shí)現(xiàn)分頁(yè)page
- 01-10Echarts實(shí)現(xiàn)單條折線可拖拽效果
- 01-10在Vue項(xiàng)目中使用Typescript的實(shí)現(xiàn)
- 01-10js實(shí)現(xiàn)上傳圖片并顯示圖片名稱
- 01-10Vue中使用Lodop插件實(shí)現(xiàn)打印功能的簡(jiǎn)單方法
- 01-10echarts實(shí)現(xiàn)折線圖的拖拽效果
- 01-10d3.js實(shí)現(xiàn)圖形縮放平移
- 01-10小程序簡(jiǎn)單兩欄瀑布流效果的實(shí)現(xiàn)
- 01-10H5實(shí)現(xiàn)手機(jī)拍照和選擇上傳功能
- 01-10Echarts實(shí)現(xiàn)多條折線可拖拽效果


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02javascript點(diǎn)線,點(diǎn)線的代碼
- 04-02javascript潛力,javascript強(qiáng)大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報(bào),JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁(yè)面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)