【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
		})
	}
相关推荐
子兮曰3 分钟前
AI Agent 完整入门指南:从 LLM 到生产落地的 30+ 个核心概念
前端·后端·agent
楚宝ovo19 分钟前
新增一个 demo 子应用 - 微前端
前端
不爱编程的小九九38 分钟前
小九源码-springboot004-springboot智能阅读推荐系统
java·spring boot·后端
SCandL1521 小时前
Docker的实战应用
java·docker·容器
JarvanMo1 小时前
Dart 3.13:小版本,大动作
前端
纯粹的热爱1 小时前
Ubuntu 安装 Node.js 22.x(通过 NodeSource 官方源)
前端
AC赳赳老秦1 小时前
OpenClaw 多源采集公开行业数据:从原始信息到研究报告初稿的自动化实践
java·c语言·c++·python·php·deepseek·openclaw
Highcharts1 小时前
如何选择正确的图表:驱动理解和洞察力的三部分框架(第3部分)
前端·数据可视化
JavaGuide2 小时前
Github 史诗级故障,与此同时,Cursor 版「GitHub」正式上线!
前端·后端
郭邯2 小时前
用 AI 写了一个经纬度格式转换工具,从需求到落地的完整过程
前端