vue實(shí)現(xiàn)移動端圖片上傳功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)移動端圖片上傳的具體代碼,供大家參考,具體內(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)第一個參數(shù)是event對象
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("請選擇您要上傳的文件!");
return false;
}else{
return true;
}
let size = Math.floor(file.size / 1024);
// 這個條件還得改
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>
在頁面當(dāng)中的使用:
<upload-img
:lab-tex="`上傳銀行卡正面`"
:img-default="imgFourDefault"
v-on:change-img="chooseFourImg"
></upload-img>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:javascript實(shí)現(xiàn)點(diǎn)擊星星小游戲
欄 目:JavaScript
下一篇:webpack打包html里面img后src為“[object Module]”問題
本文標(biāo)題:vue實(shí)現(xiàn)移動端圖片上傳功能
本文地址:http://www.jygsgssxh.com/a1/JavaScript/9417.html
您可能感興趣的文章
- 04-02java后端代碼分頁 java后端實(shí)現(xiàn)分頁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)打印功能的簡單方法
- 01-10echarts實(shí)現(xiàn)折線圖的拖拽效果
- 01-10d3.js實(shí)現(xiàn)圖形縮放平移
- 01-10小程序簡單兩欄瀑布流效果的實(shí)現(xiàn)
- 01-10H5實(shí)現(xiàn)手機(jī)拍照和選擇上傳功能
- 01-10Echarts實(shí)現(xiàn)多條折線可拖拽效果


閱讀排行
本欄相關(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警報,JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)


