快速實(shí)現(xiàn)多圖片上傳組件,支持單個文件,多個文件上傳 步驟條step使用;?閱讀全文下載完整代碼請關(guān)注微信公眾號: 前端組件開發(fā)
效果圖如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-480397.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-480397.html
使用方法
// 添加多張圖片(少于6張)
addPhotoClick() {
uni.hideLoading();
let myThis = this;
if (myThis.photoList.length >= 6) {
myThis.photoList = [];
}
uni.chooseImage({
count: 6,
sizeType: ['compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album'], //從相冊選擇
success: function(res) {
for (let j = 0; j < res.tempFilePaths.length; j++) {
// name:服務(wù)端文件接受key? filePath: 需與服務(wù)端文件接收字段保持一致
let tmpdict = {
'name': 'image',
'filePath': res.tempFilePaths[j]
};
myThis.photoList.push(tmpdict);
}
console.log('選擇圖片 =' + JSON.stringify(myThis.photoList));
}
});
}
// 服務(wù)器地址上傳地址 僅為示例,非真實(shí)的接口地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
console.log('圖片資源 = ' + JSON.stringify(this.photoList))
console.log('請求參數(shù) = ' + JSON.stringify(this.myParamData));
uni.uploadFile({
url: baseUrl, //僅為示例,非真實(shí)的接口地址
files: this.photoList,
formData: this.myParamData,
success: (uploadFileRes) => {
uni.hideLoading();
console.log('上傳成功 = ' + uploadFileRes.data);
}
});
HTML代碼部分
<template>
<view class="content" v-if="seen">
<uni-steps style="margin-top: 16px; margin-bottom: 16px;"
:options="[{title: '基本信息'}, {title: '房源信息'}, {title: '補(bǔ)充信息'}, {title: '提交成功'}]" :active="2"
active-color='#007AFF'></uni-steps>
<view class="lineView"></view>
<form @submit="formSubmit" @reset="formReset">
<view class="inputView">
<text class="leftTitle">交通指引</text>
</view>
<textarea class="rightTextarea" name="direct" placeholder=" 請輸入交通指引" />
<view class="inputView">
<text class="leftTitle">房屋介紹</text>
</view>
<textarea class="rightTextarea" name="village" placeholder=" 請輸入房屋介紹" />
<view class="inputView">
<text class="leftTitle">添加房源照片(最多可添加6張)</text>
</view>
<view class="tagView">
<!-- 自定義了一個data-id的屬性,可以通過js獲取到它的值!? hover-class 指定按下去的樣式類-->
<image class="addPhotoV" mode="aspectFill" v-for="(item, index) in photoList" :key="index"
:src="item.filePath">
</image>
<image class="addPhotoV" mode="center" @click="addPhotoClick" src="../../static/repair_camera.png">
</image>
</view>
<view class="uni-btn-v">
<button class="botBtn" type="primary" form-type="submit">提交</button>
<view class="tipText"> 注意事項(xiàng): 請確保您填寫的房屋信息真實(shí)無誤 </view>
</view>
</form>
</view>
</template>
JS代碼 (引入組件 填充數(shù)據(jù))
<script>
import Vue from 'vue';
export default {
data() {
return {
photoList: [],
seen: true,
myParamData: {},
isClick: false,
};
},
onLoad: function(e) {
if (typeof(e.myParamData) === 'string') {
this.myParamData = JSON.parse(e.myParamData);
console.log('頁面3 數(shù)據(jù) = ' + JSON.stringify(e));
}
},
methods: {
formSubmit: function(e) {
console.log('form發(fā)生了submit事件,攜帶數(shù)據(jù)為:' + JSON.stringify(e.detail.value));
if (this.isClick) {
let that = this;
setTimeout(function() {
that.isClick = false;
}, 600)
return;
}
this.isClick = true;
var formdata = e.detail.value;
this.myParamData = Object.assign(this.myParamData, formdata);
console.log('頁面3 myParamData=' + JSON.stringify(this.myParamData));
if (formdata['direct'].length < 2) {
uni.showModal({
content: '請輸入交通指引',
showCancel: false
});
return;
}
if (formdata['village'].length < 2) {
uni.showModal({
content: '請輸入所在小區(qū)介紹',
showCancel: false
});
return;
}
if (this.photoList.length < 1) {
uni.showModal({
content: '請?zhí)砑臃吭凑掌?,
showCancel: false
});
return;
}
if (this.photoList.length > 6) {
uni.showModal({
content: '最多只能選擇提交6張圖片',
showCancel: false
});
return;
}
uni.showLoading({
title: '上傳中'
})
到了這里,關(guān)于前端vue uni-app多圖片上傳組件,支持單個文件,多個文件上傳 步驟條step使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!