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
	}

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

相关推荐
星栈3 分钟前
LiveView 怎么测:从组件测试到端到端,我终于不再只敢手点页面了
前端·前端框架·elixir
宠友信息4 分钟前
Spring Boot与异步审核构建仿小红书源码内容发布全流程
java·数据库·spring boot·redis·mysql·oracle·uni-app
蜡台8 分钟前
uniapp vue2 转 vue3
前端·javascript·uni-app·vue3·vue2
Web4Browser10 分钟前
Headless 浏览器自动化适合哪些任务?巡检、截图和提交动作要分级
运维·前端·自动化
用户0595401744613 分钟前
把前端内存泄漏排查从2小时压到5分钟,我们把它集成进了CI
前端·css
WindfallSheng1 小时前
从Vibe Coding到Spec Coding:一套可落地的AI-SDD企业级研发实战工程
javascript·vue.js
Asize1 小时前
Agent 入门:从 LLM 与 Agent 的区别到 Function Calling
javascript·人工智能
申君健24864182772021 小时前
Vapor 核心数据结构:Block原理
前端
Csvn1 小时前
工程化专题 | 改完组件保存,浏览器毫无反应,重启 dev server 又好了——气不气?
前端
我有满天星辰2 小时前
Vue 指令完全指南:从 Vue 2 到 Vue 3 的演进与实战
前端·javascript·vue.js