/*** 上传文件*/uploadPhoneFile:function(callback,params = {}) {let fileType = ['.pdf','.doc','.xlsx','.docx','.xls']// #ifdef APP-PLUSplus.io.chooseFile({title: '选择文件', filetypes: ['doc', 'docx'], // 允许的文件类型 multiple: false, // 是否允许多选 },(e)=>{const tempFilePaths = e.files//第二步:把选择的文件上传到服务器Promise.all(tempFilePaths.map((tempFile) => {return new Promise((resolve,reject) => {uni.uploadFile({url: process.uniEnv.baseUrl + '/file/attch/upload',filePath: tempFile,name: 'file',header: {Authorization: 'Bearer ' + utils.CacheStorage("access_token"),// uni.getStorageSync('access_token'),terminal:'app'},success: (uploadFileRes) => {// console.log('asdaggg',JSON.parse(uploadFileRes.data));let { code,data,msg } = JSON.parse(uploadFileRes.data)if(code === 200) {resolve(data)} else {uni.showToast({title: msg,icon: 'none'})reject('上传失败')}},fail: (error) => {reject(error)}});})})).then(images => {// console.log('上传成功>>>>>>',images)callback(images)})});// #endif// #ifndef APP-PLUSuni.chooseFile({//第一步:选择文件count: 1, //默认100extension: fileType,success: function (res) {const tempFilePaths = res.tempFilePaths;//若果只选择一个文件,这个文件就是数组的第一个元素//第二步:把选择的文件上传到服务器Promise.all(res.tempFilePaths.map((tempFile) => {return new Promise((resolve,reject) => {uni.uploadFile({url: '/dev-api/file/attch/upload',filePath: tempFile,name: 'file',header: {Authorization: 'Bearer ' + utils.CacheStorage("access_token"),// uni.getStorageSync('access_token'),terminal:'app'},success: (uploadFileRes) => {console.log('asdaggg',JSON.parse(uploadFileRes.data));let { code,data,msg } = JSON.parse(uploadFileRes.data)if(code === 200) {resolve(data)} else {uni.showToast({title: msg,icon: 'none'})reject('上传失败')}},fail: (error) => {reject(error)}});})})).then(images => {console.log('asdjgllgl;s',images)callback(images)})}})// #endif}
写法很粗糙,只自己用作记录,可以再优化与扩展。