uniapp 使用uview2.0 u-upload组件上传图片文件

uview2.0文档

uni.uploadFile(OBJECT) | uni-app官网

上传图片不能使用uni.request因为data参数不支持fromData数据格式

html 复制代码
<template>
	<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="4" width="180rpx" height="180rpx">
	   <div class="addPic flex justify-center align-center"><u-icon name="plus" :bold="true" color="#172B4D" size="56rpx"></u-icon></div>
	</u-upload>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
			}
		},
		methods:{
			// 删除图片
			deletePic(event) {
				console.log(event);
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log("url", url, typeof(url));
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${config.baseUrl}/common/upload/uploadPicFile`, // 仅为示例,非真实的接口地址
						filePath: url,
						name: 'multipartFiles',//这个字段很重要,后端根据这个name获取二进制文件
						formData: {
							type: 11
						},
						success: (res) => {
							console.log("图片接口res", res);
							resolve(res.data.data)
						}
					});
				})
			},
		}

	}
</script>
相关推荐
2501_915909069 小时前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
郑州光合科技余经理10 小时前
技术视角:海外版一站式同城生活服务平台源码解析
java·开发语言·uni-app·php·排序算法·objective-c·生活
wangdaoyin201013 小时前
UniApp中使用LivePlayer进行视频或在流媒体播放
uni-app·liveplayer·h5播放视频
2501_9151063213 小时前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者814 小时前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_9160088914 小时前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
一室易安15 小时前
解决使用 UniApp 搭配 Vue3 小程序开始 使用uview-plus 的返回顶部up-back-top中onPageScroll 不触发的问题
小程序·uni-app
yilan_n15 小时前
鸿蒙应用上传
vue.js·华为·uni-app
yilan_n15 小时前
【UniApp实战】手撸面包屑导航与路由管理 (拒绝页面闪烁)
前端·javascript·vue.js·uni-app·gitcode
一室易安15 小时前
uniapp+vue3 微信小程序中 页面切换tab 页面滚动到指定锚点位置,滚动页面时候到达指定锚点位置吸顶tab 会自动进行切换
微信小程序·uni-app·notepad++