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
	}

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

相关推荐
微信开发api-视频号协议几秒前
Codex++安全边界探秘:从模型能力到风险防御
前端·安全·微信·企业微信
想你依然心痛28 分钟前
AtomCode 在前端开发中的实战体验:React + TypeScript 项目开发实录
前端·react.js·typescript
疯狂的魔鬼31 分钟前
精确计算容器剩余视口高度:useAutoContainerFullHeight 的工程实践
前端·css·typescript
Esaka_Forever37 分钟前
Python 与 JS (V8) 垃圾回收核心区别 + 底层根源分析
开发语言·javascript·jvm
用户0595401744639 分钟前
用了 3 个月 ChatGPT,才发现它一直在遗忘——用 Playwright 自动化验证记忆存储一致性
前端·css
玄玄子39 分钟前
xss前端解决方案
前端·浏览器·xss
林希_Rachel_傻希希42 分钟前
web性能优化之——AI总结视频
前端·javascript·面试
前端炒粉1 小时前
个人简历面经总结二
前端·网络·vue.js·react.js·面试
binbin_521 小时前
UIAbility 与 WindowStage:窗口创建、加载、销毁的完整链路
开发语言·javascript·深度学习·华为·harmonyos
用户059540174461 小时前
用了半年 LangChain Memory,才发现回滚测试压根没测对
前端·css