小程序端代码
uploadfile函数:选择图片,然后循环调用uploadfilepath方法,达到上传多张的效果
uploadfilepath函数:上传图片专用的,并且处理上传完成后要做的事情(例如显示在页面里)
,
uploadfile(){
let that=this
//选取图片
wx.chooseImage({
count: 3,
sizeType: ['original'],//原图
sourceType: [ 'album','camera'],//支持选取图片
success (res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths[0];
for (const key in res.tempFilePaths) {
if (Object.hasOwnProperty.call(res.tempFilePaths, key)) {
const element = res.tempFilePaths[key];
that.uploadfilepath(element)
}
}
}
})
},
uploadfilepath(tempFilePaths){
//上传图片
wx.uploadFile({
//请求后台的网址
url: gb.apidomain +'/common/upload',
//小程序本地的路径
filePath: tempFilePaths,
//后台获取我们图片的key
name: 'file',
//额外的参数formData
formData: {
'from': 'xcx'
},
header: {
token:gb.token
},
success: function (res) {
//上传成功
console.log(res,'success');
},
fail: function (res) {
console.log(res,'fail');
},
})
}
注意:gb.apidomain是服务器端的域名,formData里可以附带额外参数,count指的是最多一次能上传几张
php后端代码
后端代码是fastadmin自带的,不需要我们写了,路径是application/api/controller/Common.php里的upload方法
上传完成的结果:
相关文档:
wx.uploadFile https://developers.weixin.qq.com/minigame/dev/api/network/upload/wx.uploadFile.html