⼩程序上传多个图⽚源码
开发⼩程序,如果 除了图⽚外,还有其他⼏个⽂本表单,想着⼀起上传,⼩程序只能先把图⽚给传上去,然后再提交表单了。如果上传多张图⽚,是采⽤循环上传图⽚的⽅式。
const uploadFile = (filePath, clear = false, fullPath = false) => {
return new Promise((resolve, reject) => {
getEnv().then((res) => {
if (!filePath || filePath.length < 9) {
wx.showModal({
title: '图⽚错误',
图片下载站源码content: '请重试',
showCancel: false,
})
return;
}
const fileType = '.' + filePath.split('.')[filePath.split('.').length - 1]
const aliyunFileKey = res.dir + new Date().getTime() + Math.floor(Math.random() * 150) + fileType;
const aliyunServerURL = res.host;
const accessid = res.accessId;
wx.uploadFile({
url: aliyunServerURL, //开发者服务器 url
filePath: filePath, //要上传⽂件资源的路径
name: 'file', //必须填file
formData: {
'key': aliyunFileKey,
'policy': res.policy,
'OSSAccessKeyId': accessid,
'signature': res.signature,
'success_action_status': '200',
},
success: function (res) {
if (res.statusCode != 200) {
reject();
}
if (clear) {
clearEnv()
}
if (fullPath)
resolve(aliyunServerURL + '/' + aliyunFileKey) // 全路径上传
else
resolve(aliyunFileKey) // 残路径上传
},
fail: function (err) {
err.wxaddinfo = aliyunServerURL;
failc(err);
},
})
})
})
}
下载取1000个⼩程序打包源码:
// 上传图⽚
upload(imageList, imageSrcList = []) {
let that = this
if (imageSrcList == []) {
imageList.forEach(() => {
imageSrcList.push([])
})
}
let clear = (times == imageList.length - 1)
uploadImage(imageList[times], clear).then((result) => {      wx.hideLoading();
times++
wx.showLoading({
title: times + '/' + imageList.length
})
imageSrcList[times - 1] = result
if (times == imageList.length) {
times = 0
that.addPhoto(imageSrcList)
} else {
that.upload(imageList, imageSrcList)
}
})
},

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。