【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
		})
	}
相关推荐
Flynt11 分钟前
axios 被投毒半年后,我把 npm 的"安全防线"实测了一遍:最信任的 SHA512 是安慰剂
前端·npm·node.js
excel1 小时前
Base62 和 Base64 有什么区别?
前端
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 14 章 MCP 服务端(上):注解式实现与工具来源
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 15 章 MCP 服务端(下):三种协议形态、Streamable 传输与状态管理
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 16 章 向量存储(上):`VectorStore` 接口体系、`BatchingStrategy`
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 12 章 工具循环的流式形态、Usage 累加与扩展体系
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 23 章 模型供应商实现:从 `OpenAiSetup` 到 Bedrock
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 06 章 内置 Advisor 全景:13 个实现逐个剖到源码级
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 03 章 消息体系与 Prompt:不可变模型、`ChatOptions` 与模板渲染
java·人工智能·开源
鶴哥只手遮天1 小时前
Spring AI 2.0.1 深度解析-第 24 章 生产实践与 2.x 迁移:陷阱清单、AOT 与调优
java·人工智能·开源