uniapp APP 上传文件

javascript 复制代码
/**
	 * 上传文件
	 */
	uploadPhoneFile:function(callback,params = {}) {
		let fileType = ['.pdf','.doc','.xlsx','.docx','.xls']
		// #ifdef APP-PLUS
			plus.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-PLUS
		uni.chooseFile({
			//第一步:选择文件
			count: 1, //默认100
			extension: 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
	}

写法很粗糙,只自己用作记录,可以再优化与扩展。

相关推荐
Jiaberrr1 小时前
前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)
前端·javascript·vue.js·微信小程序·uni-app
everyStudy1 小时前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
城南云小白1 小时前
web基础+http协议+httpd详细配置
前端·网络协议·http
前端小趴菜、1 小时前
Web Worker 简单使用
前端
web_learning_3211 小时前
信息收集常用指令
前端·搜索引擎
Ylucius2 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
tabzzz2 小时前
Webpack 概念速通:从入门到掌握构建工具的精髓
前端·webpack
200不是二百2 小时前
Vuex详解
前端·javascript·vue.js
滔滔不绝tao2 小时前
自动化测试常用函数
前端·css·html5
LvManBa2 小时前
Vue学习记录之三(ref全家桶)
javascript·vue.js·学习