wx.uploadFile()如何封装promise?
wx.uploadFile()不支持多图片上传,
只能用递归。
打算封装成promise
但是我既想resolve出success中的结果,又想监听到uploadTask的上传进度
我该怎么办呢?
resolve 不知道放在哪?
// 上传图片 const uploadImg = (filePath) => { return new Promise((resolve, reject) => { const uploadTask = wx.uploadFile({ url: app.globalData.baseUrl + '/wechat/app/common/uploadImage' , filePath: filePath, name: 'file' , formData: { 'path' : 'm0' }, success: function (res) { console.log(res, 888) let d = JSON.parse(res.data) console.log(d)
console.log(d.data) } else { wx.showToast({ title: '当前网络不佳,请稍后重试' , icon: 'none' }) } }, fail(err) { wx.showToast({ title: '当前网络不佳,请稍后重试' , icon: 'none' }) } }) uploadTask.onProgressUpdate((res) => { // resolve(res) console.log(res) }) }) } |
2 回复
let app = getApp(); module.exports = function (url, filePath) { return new Promise((resolve, reject) => { wx.uploadFile({ url: `${app.globalData.apiUrl}sys/${url}`, filePath, name: 'file' , formData: { 'user' : 'test' }, method: 'POST' , header: { 'Authorization' : `Bearer${app.globalData.userToken}` }, success: resolve, fail: reject }) }) } |
const uploadFile = require('../../utils/uploadFile.js');
let uploads = []; files.forEach(ele => { uploads.push(uploadFile( 'upload' , ele))
|
Promise.all(uploads).then(res=>{ //todo }) |