基于uniapp开发的微信H5图片上传压缩

安装Compressor,并在页面引入

复制代码
npm i compressorjs

	import Compressor from "compressorjs"

具体使用代码

H5部分:

html 复制代码
<view class="h5upload" @click="add">
								<view class="">
									+
								</view>
								<view class="">
									上传图片
								</view>
							</view>

js部分:

javascript 复制代码
//选择图片
			async add() {
				const input = document.createElement('input');
				input.type = 'file';
				input.accept = 'image/*';
				input.multiple = true;

				input.onchange = async (event) => {
					const files = Array.from(event.target.files);
					let arr = [];
					let promiseArr = [];
					uni.showLoading({
						title: "上传中"
					});

					for (const file of files) {
						try {
							const compressedBlob = await this.compressImage(file);
							console.log("图片压缩后", compressedBlob);

							// 将 Blob 转换为 File 对象
							const fileName = file.name; // 保持原文件名
							const fileType = compressedBlob.type; // 获取 Blob 类型
							const fileToUpload = new File([compressedBlob], fileName, {
								type: fileType
							});

							// 使用 FormData 来上传文件
							const formData = new FormData();
							formData.append('file', fileToUpload);
							formData.append('user', 'test');

							promiseArr.push(fetch(`${IMG_BASE_URL}/photo_album`, {
									method: 'POST',
									body: formData,
								})
								.then(response => {
									if (!response.ok) {
										throw new Error(`网络错误: ${response.status}`);
									}
									return response.json();
								})
								.then(data => {
									console.log("上传响应数据:", data);
									return data?.data?.path || "";
								})
								.catch(error => {
									console.error("上传失败", error);
									throw error;
								}));
						} catch (error) {
							console.error("压缩失败", error);
						}
					}

					const results = await Promise.all(promiseArr);
					console.log("上传图片数组", results);
					uni.hideLoading();
					this.fileList = [...this.fileList, ...results]
				};

				input.click();
			},
    //压缩
	compressImage(file) {
				return new Promise((resolve, reject) => {
					new Compressor(file, {
						quality: 0.7,//压缩等级
						success(result) {
							resolve(result);
						},
						error(err) {
							reject(err);
						},
					});
				});
			},
相关推荐
于先生吖9 小时前
UniApp搭配Java后端实现到店预约上门指派,订单状态流转与结算开发教程
java·开发语言·uni-app
岳哥i12 小时前
uniapp打包原生App流程及兼容性适配
uni-app
niech_cn13 小时前
uniapp开发App(iOS、Android、鸿蒙Next)之配置pages.json 页面路由(三)
android·ios·uni-app
郑州光合科技余经理15 小时前
海外版外卖系统源码:支付/地图/多语言核心代码实现
android·java·前端·后端·架构·uni-app·php
游戏开发爱好者815 小时前
Linux 自动上传 App Store Connect:把 IPA 上传流程接进CI工作流
linux·运维·ios·ci/cd·小程序·uni-app·iphone
暗冰ཏོ15 小时前
《uni-app 跨端开发完整指南:从基础入门到 H5、小程序、App 发布上线》
前端·小程序·uni-app·vue·html5
梦梦代码精16 小时前
TP8+Vue3+UniApp:LikeShop架构受青睐!
架构·uni-app
暗冰ཏོ16 小时前
2026 App 开发完整指南:Android、iOS、跨平台开发与安卓应用上线全流程
android·ios·uni-app·web app·app开发
Geek_Vison2 天前
技术实践:保险健康APP引入第三方小程序实战,如何构建一个安全可控的沙箱环境~
android·安全·小程序·uni-app·mpaas
2501_915918412 天前
Python如何抓取HTTPS请求包的完整教程与代码示例
android·ios·小程序·https·uni-app·iphone·webview