- 首先在util.js里封装图片选择函数
function Image( count , successFn , failFn ){//上传图片,count确定上传图片的张数
var that = this;
wx.chooseImage({
count: count,
sizeType: [‘original’, ‘compressed’],
sourceType: [ ‘album’ , ‘camera’ ],
‘success’:(res) =>{
successFn(res)
},
‘fail’:(err)=>{
failFn(err)
}
})
}
module.exports = {//导出函数
//formatTime: formatTime,
Image
}
- 在需要的js文件中引入函数
var utils = require(‘../../utils/util.js’);
upLoad(e){//图片上传
var that = this;
utils.Image(9,res=>{
wx.showLoading({
title: ‘正在提交’,
})
if(that.data.imgSrc.length <= 9){
that.setData({imgSrc: that.data.imgSrc.concat(res.tempFilePaths)})
for(let i=0; i< that.data.imgSrc.length; i++){<br /> wx.uploadFile({<br /> url: 'http://localhost', //图片上传的url<br /> filePath: res.tempFilePaths[i],<br /> name: 'file', <br /> success (res){<br /> wx.hideLoading();
},<br /> fail (err){<br /> wx.hideLoading();
}<br /> })<br /> }
}else{<br /> wx.showModal({<br /> title:'提示',<br /> content:'最多上传九张图片'<br /> })<br /> }<br /> },err=>{})<br /> },