前端vue單個文件上傳支持圖片,壓縮包以及文件?, 閱讀全文下載完整代碼請關(guān)注微信公眾號: 前端組件開發(fā)
效果圖如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-486323.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-486323.html
使用方法
<!-- photoList:選擇的圖片數(shù)組? @click:圖片選擇事件-->
<ccChooseImgsView :photoList="photoList" @click="addPhotoClick"></ccChooseImgsView>
addPhotoClick() {
uni.hideLoading();
let myThis = this;
if (myThis.photoList.length >= 1) {
myThis.photoList = [];
}
uni.chooseImage({
count: 1,
sizeType: ['compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album'], //從相冊選擇
success: function(res) {
myThis.photoList = [{'filePath':res.tempFilePaths[0]}];
console.log('選擇圖片 =' + JSON.stringify(myThis.photoList));
}
});
},
if (this.photoList.length > 0) {
myFilePath = this.photoList[0].filePath;
}
// 服務(wù)器地址上傳地址 僅為示例,非真實(shí)的接口地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
uni.uploadFile({
url: baseUrl, //僅為示例,非真實(shí)的接口地址
filePath: myFilePath, //文件路徑
name: 'image', //服務(wù)端文件接受key
formData: this.myParamData,
success: (uploadFileRes) => {
uni.hideLoading();
let dataDic = JSON.parse(uploadFileRes.data);
console.log('uploadFileRes成功 = ' + JSON.stringify(uploadFileRes));
console.log('datadic成功 = ' + typeof(dataDic));
console.log('code碼 = ' + dataDic['code']);
}
});
HTML代碼部分
<template>
<view class="content" v-if="seen">
<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">添加房源照片(僅可添加1張)</text>
</view>
<!-- photoList:選擇的圖片數(shù)組? @click:圖片選擇事件-->
<ccChooseImgsView :photoList="photoList" @click="addPhotoClick"></ccChooseImgsView>
<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';
import ccChooseImgsView from '../../components/ccChooseImgsView.vue';
export default {
components: {
ccChooseImgsView
},
data() {
return {
photoList: [],
seen: true,
myParamData: {},
isClick: false,
};
},
onLoad: function(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;
}
uni.showLoading({
title: '上傳中'
})
let myFilePath = '';
if (this.photoList.length > 0) {
myFilePath = this.photoList[0].filePath;
}
// 服務(wù)器地址上傳地址 僅為示例,非真實(shí)的接口地址
let baseUrl = "http://gzcc.com/cc//appSc/up"
uni.uploadFile({
url: baseUrl, //僅為示例,非真實(shí)的接口地址
filePath: myFilePath, //文件路徑
name: 'image', //服務(wù)端文件接受key
formData: this.myParamData,
success: (uploadFileRes) => {
uni.hideLoading();
let dataDic = JSON.parse(uploadFileRes.data);
console.log('uploadFileRes成功 = ' + JSON.stringify(uploadFileRes));
console.log('datadic成功 = ' + typeof(dataDic));
console.log('code碼 = ' + dataDic['code']);
}
});
},
addPhotoClick() {
uni.hideLoading();
let myThis = this;
if (myThis.photoList.length >= 1) {
myThis.photoList = [];
}
uni.chooseImage({
count: 1,
sizeType: ['compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
sourceType: ['album'], //從相冊選擇
success: function(res) {
myThis.photoList = [{'filePath':res.tempFilePaths[0]}];
console.log('選擇圖片 =' + JSON.stringify(myThis.photoList));
}
});
},
}
};
</script>
CSS
<style>
.uni-form-item .title {
padding: 20rpx 0;
}
.content {
display: flex;
flex-direction: column;
width: 100%;
height: auto;
}
.inputView {
flex-direction: row;
display: flex;
height: 40px;
align-items: center;
width: 100%;
}
.line {
width: 90%;
height: 2rpx;
margin-left: -2rpx;
background-color: #f8f8f8;
margin-left: 5%;
}
.leftTitle {
margin-left: 40rpx;
width: 284px;
height: 32px;
line-height: 32px;
font-size: 28rpx;
color: #333333;
}
.rightTextarea {
margin-left: 5%;
width: 90%;
height: 106px;
line-height: 40rpx;
border-radius: 12rpx;
border: solid 1px #F5F5F5;
font-size: 15px;
}
.uni-btn-v {
width: 100%;
height: auto;
}
.botBtn {
width: 90%;
margin-top: 36px;
height: 48px;
}
.tipText {
width: 100%;
margin-left: 0px;
text-align: center;
color: #666666;
margin-top: 36px;
margin-bottom: 36px;
font-size: 28rpx;
}
</style>
到了這里,關(guān)于前端vue單個文件上傳支持圖片,壓縮包以及文件 , 下載完整代碼請?jiān)L問uni-app插件市場址:https://ext.dcloud.net.cn/plugin?id=13066的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!