快速實作多圖片上傳組件,支持單個檔案,多個檔案上傳 步驟條step使用; 下載完整代碼請訪問uni-app插件市場地址:https://ext.dcloud.net.cn/plugin?id=12747
效果圖如下:
使用方法
使用方法
// 添加多張圖片(少于6張)
addPhotoClick() {
uni.hideLoading();
let myThis = this;
if (myThis.photoList.length >= 6) {
myThis.photoList = [];
}
uni.chooseImage({
count: 6,
sizeType: ['compressed'], //可以指定是原圖還是壓縮圖,默認二者都有
sourceType: ['album'], //從相冊選擇
success: function(res) {
for (let j = 0; j < res.tempFilePaths.length; j++) {
// name:服務端檔案接受key filePath: 需與服務端檔案接收欄位保持一致
let tmpdict = {
'name': 'image',
'filePath': res.tempFilePaths[j]
};
myThis.photoList.push(tmpdict);
}
console.log('選擇圖片 =' + JSON.stringify(myThis.photoList));
}
});
}
// 服務器地址上傳地址 僅為示例,非真實的介面地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
console.log('圖片資源 = ' + JSON.stringify(this.photoList))
console.log('請求引數 = ' + JSON.stringify(this.myParamData));
uni.uploadFile({
url: baseUrl, //僅為示例,非真實的介面地址
files: this.photoList,
formData: this.myParamData,
success: (uploadFileRes) => {
uni.hideLoading();
console.log('上傳成功 = ' + uploadFileRes.data);
}
});
HTML代碼部分
html
<template>
<view class="content" v-if="seen">
<uni-steps style="margin-top: 16px; margin-bottom: 16px;"
:options="[{title: '基本資訊'}, {title: '房源資訊'}, {title: '補充資訊'}, {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=https://www.cnblogs.com/ccVue/archive/2023/06/10/"item.filePath">
</image>
<image class="addPhotoV" mode="center" @click="addPhotoClick" src=https://www.cnblogs.com/ccVue/archive/2023/06/10/"../../static/repair_camera.png">
</image>
</view>
<view class="uni-btn-v">
<button class="botBtn" type="primary" form-type="submit">提交</button>
<view class="tipText"> 注意事項: 請確保您填寫的房屋資訊真實無誤 </view>
</view>
</form>
</view>
</template>
JS代碼 (引入組件 填充資料)
javascript
<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 = https://www.cnblogs.com/ccVue/archive/2023/06/10/JSON.parse(e.myParamData);
console.log('頁面3 資料 = ' + JSON.stringify(e));
}
},
methods: {
formSubmit: function(e) {
console.log('form發生了submit事件,攜帶資料為:' + JSON.stringify(e.detail.value));
if (this.isClick) {
let that = this;
setTimeout(function() {
that.isClick = false;
}, 600)
return;
}
this.isClick = true;
var formdata = https://www.cnblogs.com/ccVue/archive/2023/06/10/e.detail.value;
this.myParamData = https://www.cnblogs.com/ccVue/archive/2023/06/10/Object.assign(this.myParamData, formdata);
console.log('頁面3 myParamData='https://www.cnblogs.com/ccVue/archive/2023/06/10/+ JSON.stringify(this.myParamData));
if (formdata['direct'].length < 2) {
uni.showModal({
content: '請輸入交通指引',
showCancel: false
});
return;
}
if (formdata['village'].length < 2) {
uni.showModal({
content: '請輸入所在小區介紹',
showCancel: false
});
return;
}
if (this.photoList.length < 1) {
uni.showModal({
content: '請添加房源照片',
showCancel: false
});
return;
}
if (this.photoList.length > 6) {
uni.showModal({
content: '最多只能選擇提交6張圖片',
showCancel: false
});
return;
}
uni.showLoading({
title: '上傳中'
})
// 服務器地址上傳地址 僅為示例,非真實的介面地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
