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對象
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);
// 這個(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>
在頁面當(dāng)中的使用:
<upload-img :lab-tex="`上傳銀行卡正面`" :img-default="imgFourDefault" v-on:change-img="chooseFourImg" ></upload-img>
關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue移動(dòng)端實(shí)現(xiàn)圖片上傳及超過1M壓縮上傳
- 移動(dòng)端 Vue+Vant 的Uploader 實(shí)現(xiàn)上傳、壓縮、旋轉(zhuǎn)圖片功能
- Vue2.0 實(shí)現(xiàn)移動(dòng)端圖片上傳功能
- Vue的移動(dòng)端多圖上傳插件vue-easy-uploader的示例代碼
- vue實(shí)現(xiàn)移動(dòng)端圖片裁剪上傳功能
- vue+vux實(shí)現(xiàn)移動(dòng)端文件上傳樣式
- Vue.js 2.0 移動(dòng)端拍照壓縮圖片預(yù)覽及上傳實(shí)例
- vue2實(shí)現(xiàn)移動(dòng)端上傳、預(yù)覽、壓縮圖片解決拍照旋轉(zhuǎn)問題
- Vue.js 2.0 移動(dòng)端拍照壓縮圖片上傳預(yù)覽功能
- vue實(shí)現(xiàn)移動(dòng)端input上傳視頻、音頻
相關(guān)文章
vue實(shí)現(xiàn)echarts餅圖/柱狀圖點(diǎn)擊事件實(shí)例
echarts原生提供了相應(yīng)的API,只需要在配置好echarts之后綁定相應(yīng)的事件即可,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)echarts餅圖/柱狀圖點(diǎn)擊事件的相關(guān)資料,需要的朋友可以參考下2023-06-06
Vue-element-admin平臺側(cè)邊欄收縮控制問題
這篇文章主要介紹了Vue-element-admin平臺側(cè)邊欄收縮控制問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
uniapp和vue組件之間的傳值(父子傳值,兄弟傳值,跨級傳值,vuex)
這篇文章主要介紹了uniapp和vue組件之間的傳值(父子傳值,兄弟傳值,跨級傳值,vuex),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化
在前端開發(fā)中,打印功能是一個(gè)常見的需求,但不同瀏覽器對打印樣式的支持差異較大,本文我們來看看Vue如何使用iframe實(shí)現(xiàn)瀏覽器打印兼容性優(yōu)化吧2025-04-04
vue中el-tree增加節(jié)點(diǎn)后如何重新刷新
這篇文章主要介紹了vue中el-tree增加節(jié)點(diǎn)后如何重新刷新,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

