1. 上传普通文件
uni.chooseMessageFile({count: 1,success: (res) => {console.log(res)console.log("res123456", res.tempFiles[0].path)const uploadTask = uni.uploadFile({url: 'http://localhost:8000/demo',filePath: res.tempFiles[0].path,name: 'file',formData: {name: "zhangsan",age: 18},success: (res) => {console.log("res123456", res.data)},fail: (err) => {console.log("err", err)}})uploadTask.onProgressUpdate(res => {console.log('上传进度' + res.progress);console.log('已经上传的数据长度' + res.totalBytesSent);console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);})}})
2. 上传图片
uni.chooseImage({count: 1,sizeType: ['original', 'compressed'],sourceType: ['album', 'camera'],success: (res) => {console.log("res123456", res.tempFilePaths)const uploadTask = uni.uploadFile({url: 'http://localhost:8000/demo',filePath: res.tempFilePaths[0],name: 'file',formData: {name: "zhangsan",age: 18},success: (res) => {console.log("res123456", res.data)},fail: (err) => {console.log("err", err)}})uploadTask.onProgressUpdate(res => {console.log('上传进度' + res.progress);console.log('已经上传的数据长度' + res.totalBytesSent);console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);})}})
参考文档