微信小程序官網社區有申明微信小程序沒法多文件上傳,因此要經過遞歸進行多文件上傳。
網上得上傳例子都不知足個人要求,以下是個人代碼,但願能給予參考。小程序
方法upload單文件上傳 方法uploadFils多文件上傳微信小程序
export function uploadFils(url, filePathArr) { return uploadFilsSync(url, filePathArr, 0, { success: [], fail: [] }); } function uploadFilsSync(url, filePathArr, index, result) { return upload(url, filePathArr[index++], {}) .then(res => { result.success.push({ index: index, res: res }); if (index === filePathArr.length) { return result; } else { return uploadFilsSync(url, filePathArr, index, result); } }) .catch(err => { result.fail.push({ index: index, res: err }); if (index === filePathArr.length) { return result; } else { return uploadFilsSync(url, filePathArr, index, result); } }); } export function upload(url, filePath, formData = {}) { let header = { "Content-Type": "multipart/form-data" }; return new Promise((resolve, reject) => { wx.uploadFile({ url: config.server + url, header: header, filePath: filePath, name: "file", formData: formData, success(res) { resolve(res.data); }, fail: function(error) { reject(error); } }); }); }
返回結果:微信
index:第幾個上傳文件,res作爲上傳結果 { "success":[ { "index":1, "res":{ "data":"123" } } ], "fail":[ ] }