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>
關(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移動端實(shí)現(xiàn)圖片上傳及超過1M壓縮上傳
- 移動端 Vue+Vant 的Uploader 實(shí)現(xiàn)上傳、壓縮、旋轉(zhuǎn)圖片功能
- Vue2.0 實(shí)現(xiàn)移動端圖片上傳功能
- Vue的移動端多圖上傳插件vue-easy-uploader的示例代碼
- vue實(shí)現(xiàn)移動端圖片裁剪上傳功能
- vue+vux實(shí)現(xiàn)移動端文件上傳樣式
- Vue.js 2.0 移動端拍照壓縮圖片預(yù)覽及上傳實(shí)例
- vue2實(shí)現(xiàn)移動端上傳、預(yù)覽、壓縮圖片解決拍照旋轉(zhuǎn)問題
- Vue.js 2.0 移動端拍照壓縮圖片上傳預(yù)覽功能
- vue實(shí)現(xiàn)移動端input上傳視頻、音頻
相關(guān)文章
vue 判斷頁面是首次進(jìn)入還是再次刷新的實(shí)例
這篇文章主要介紹了vue 判斷頁面是首次進(jìn)入還是再次刷新的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
element-ui 插槽自定義樣式居中效果實(shí)現(xiàn)思路
這篇文章主要介紹了element-ui 插槽自定義樣式居中效果,簡單來講實(shí)現(xiàn)思路是通過template標(biāo)簽可理解為一個內(nèi)嵌組件,寬高重新定義,可在自定義內(nèi)容外層套一層盒子,讓盒子占滿所有空間,再使用flex讓內(nèi)部元素居中,需要的朋友可以參考下2024-07-07
手把手教你如何將html模板資源轉(zhuǎn)為vuecli項目
Vue可以直接集成html,Vue就是前端框架,使用Vue做前端開發(fā)效率非常高,下面這篇文章主要給大家介紹了關(guān)于如何將html模板資源轉(zhuǎn)為vuecli項目的相關(guān)資料,需要的朋友可以參考下2023-04-04
vue-cli或vue項目利用HBuilder打包成移動端app操作
這篇文章主要介紹了vue-cli或vue項目利用HBuilder打包成移動端app操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vite項目配置less全局樣式的實(shí)現(xiàn)步驟
最近想實(shí)現(xiàn)個項目,需要配置全局less,本文主要介紹了vite項目配置less全局樣式的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
Vue對Element中el-tab-pane添加@click事件無效問題解決
這篇文章主要給大家介紹了關(guān)于Vue對Element中el-tab-pane添加@click事件無效問題的解決辦法,文中通過圖文以及代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

