【uniapp】使用uviewplus来实现图片上传和图片预览功能

复制代码
<up-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" @click-preview="showImg" name="1" multiple :maxCount="5"
				style="margin-top: 20px;min-height: 100px; margin-left: 10px;margin-right: 10px;" uploadText="图片上传" :previewFullImage="true"></up-upload>

新增图片

复制代码
	// 新增图片
	const afterRead = async (event) => {
		// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = fileList1.value.length;
		lists.map((item) => {
			fileList1.value.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const result = await uploadFilePromise(lists[i].url);
			
			let item = fileList1.value[fileListLen];
			fileList1.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}
		
	};

	const uploadFilePromise = (url) => {
		imgShowLoading()
		var base = getApp().globalData.baseUrl;
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: base + 'device/deviceTaskFile/upload',
				filePath: url,
				name: 'file',
				header: {
					'X-Access-Token': getAccessToken()
				},
				formData: {
					user: 'test',
				},
				success: (res) => {
					var json = JSON.parse(res.data)
					var message = json.message;
					setTimeout(() => {
						baseHideLoading()
						resolve(message);
					}, 1000);
				},
			});
		});
	};

删除图片

复制代码
	// 删除图片
	const deletePic = (event) => {
		fileList1.value.splice(event.index, 1);
	};

显示预览

复制代码
const showImg=(event) => {
		const baseUrl = getApp().globalData.baseUrl + "sys/common/static/"
		const url=[baseUrl+event.url]
		uni.previewImage({
			current: 0,
			urls:url
		})
	}
相关推荐
武子康13 小时前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
m0_6398171513 小时前
基于springboot火锅店管理系统【带源码和文档】
java·spring boot·后端
金木讲编程13 小时前
Claude、Agent与Copilot协作生成Angular应用
前端·ai编程
振华OPPO14 小时前
Vue:“onMounted“ is defined but never used no-unused-vars
前端·javascript·css·vue.js·前端框架
会编程的林俊杰14 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
欧雷殿14 小时前
在富阳银湖成立地域化的软件研发团队
前端·程序员·创业
一叶飘零_sweeeet14 小时前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
2501_9159184115 小时前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
王家羽翼-王羽15 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
狂炫冰美式15 小时前
前端实时推送 & WebSocket 面试题(2026版)
前端·http·面试