react跟taro的小程序一次性上传多张图片
react,taro,小程序上传多张图片
·
export function apiUploadImage(token: string, type: 0 | 1 | 2 | 3 | 4 | 5, count = 9): Promise<{
fileUrl: string
filePath: string
}[]> {
return new Promise((resolve, reject) => {
Taro.chooseMedia({
mediaType: ['image'],
count: count,
sourceType: ['album', 'camera'],
camera: 'front',
}).then(data => {
const file = data.tempFiles
if (!file) return;
const promises = data.tempFiles.map(image => {
return new Promise((resolve, reject) => {
Taro.uploadFile({
header: {
Authorization: token
},
url: configData.host + `/api/xcx/file/upload/${type}`,
name: 'files',
filePath: image.tempFilePath
}).then(result => {
if (result.statusCode === 200) {
const data = JSON.parse(result.data);
if (data.code === 'STATUS_000') {
resolve(data.data[0]);
} else {
reject(data.msg)
}
} else {
reject(result.errMsg)
}
}).catch(err => reject(err))
})
})
return Promise.all(promises).then(data => {
resolve(data as any)
})
}).catch(err => reject(err))
})
用遍历的方式发送就可以一次性上传多张
更多推荐
已为社区贡献3条内容
所有评论(0)