微信小程序实现下载 上传表格(xls、xlsx)

项目使用uniapp写的 专门给微信小程序做个上传 下载表格,下载方通过临时保存文件然后分享这个文件路径,达到下载功能。

远程请求路径

复制代码
const baseUrl = '' //替换成请求地址

上传方法uploading()

复制代码
const uploading = (() => {
		// 小程序
		const data = {
			后端需要的参数
			file: ''//上传路径
		};

		// 支持的表格扩展名
		const acceptedExtensions = ['xlsx', 'xls', 'csv'];

		uni.chooseMessageFile({
			count: 1,
			type: 'file', // 必须为 'file'
			extension: acceptedExtensions, // ✅ 关键:限制文件扩展名
			success: (res) => {
				const file = res.tempFiles[0];
				if (!file) {
					uni.showToast({
						title: '未选择文件',
						icon: 'none'
					});
					return;
				}

				// 额外校验(防止 extension 失效)
				const fileName = file.name || '';
				const ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
				if (!acceptedExtensions.includes(ext)) {
					uni.showToast({
						title: '仅支持 Excel 或 CSV 文件',
						icon: 'none',
						duration: 1000,
					});

					return;
				}

				// 开始上传
				uni.showLoading({
					title: '上传中...',
					mask: true
				});

				const uploadTask = uni.uploadFile({

					url: baseUrl + 'outsideProvince/importOutsideList', //接口
					filePath: file.path, // ✅ 临时文件路径
					dataType: 'json',
					name: 'file', // 后端接收字段名
					formData: {
						shareId: data.shareId,
						phone: data.phone,
						name: data.name,
						file
					},
					success: (uploadRes) => {
						uni.hideLoading();
						let resData;
						resData = JSON.parse(uploadRes.data);
						console.log(resData.code == 200, '0000', resData)
						if (resData.code == 200) {

							if (resData.warn) {
								uni.showModal({
									icon: 'exception',
									content: resData.warn, // 支持长文本、换行
									showCancel: false,

								})
							} else {
								uni.showToast({
									title: '上传完成',
									icon: 'success',
									duration: 2000,
								});
							}
							var datas = {
								shareId: person.shareId,
								search: person.searchVal,
								phone: person.phone,


							};
							selectOutsideList(datas).then((res) => {

								if (res.code == 200) {
									person.productList = res.data

								}
							})
						} else {
							uni.showToast({
								title: resData.msg || '上传失败',
								icon: 'none',
								duration: 1000,
							});
						}

					},
					fail: (err) => {
						uni.hideLoading();
						console.error('上传失败:', err);
						uni.showToast({
							title: '上传失败,请重试',
							icon: 'none',
							duration: 1000,
						});
					}
				});

				// 安全监听进度
				if (uploadTask && typeof uploadTask.onProgressUpdate === 'function') {
					uploadTask.onProgressUpdate((progressRes) => {
						console.log('上传进度:', progressRes.progress + '%');
						
						// 可选:更新页面进度 this.progress = progressRes.progress;
					});
				}
			},
			fail: (err) => {
				console.error('选择文件失败:', err);
				uni.showToast({
					title: '取消选择或选择失败',
					icon: 'none'
				});
			}
		});

下载方法download(),通过临时保存文件然后分享这个文件路径,达到下载功能。

复制代码
const download = () => {
		// const shareId = '1447090'; //后端需要的参数
		// const phone = '123';//后端需要的参数
		
		// 	// 替换为你的实际接口地址
		const baseUrls = baseUrl + 'outsideProvince/exportOutsideList';
		const url = `${baseUrls}?shareId=${encodeURIComponent(shareId)}&phone=${encodeURIComponent(phone)}`;

		uni.showLoading({
			title: '生成文件...'
		});

		uni.downloadFile({
			url,
			success: (res) => {
				uni.hideLoading();
				if (res.statusCode !== 200) {
					uni.showToast({
						title: `下载失败(${res.statusCode})`,
						icon: 'none'
					});
					return;
				}

				// 仅在微信小程序中使用 wx.shareFileMessage
				// #ifdef MP-WEIXIN
				if (typeof wx !== 'undefined' && wx.shareFileMessage) {
					wx.shareFileMessage({
						filePath: res.tempFilePath,
						fileName: '报价.xlsx',
						success: () => {
							uni.showToast({
								title: '分享成功',
								icon: 'success'
							});
						},
						fail: (err) => {
							console.error('分享失败:', err);
							uni.showToast({
								title: '分享失败',
								icon: 'none'
							});
						}
					});
				} else {
					uni.showToast({
						title: '当前环境不支持文件分享',
						icon: 'none'
					});
				}
				// #endif

				// #ifndef MP-WEIXIN
				// 其他平台可考虑保存到本地或提示用户
				uni.showToast({
					title: '仅微信小程序支持分享文件',
					icon: 'none'
				});
				// 或调用 uni.saveFile + uni.openDocument 等方式预览
				// #endif
			},
			fail: (err) => {
				uni.hideLoading();
				console.error('下载失败:', err);
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				});
			}
		});
	};
相关推荐
千寻技术帮1 小时前
50030_基于微信小程序的生鲜配送系统
mysql·微信小程序·源码·安装·文档·ppt·答疑
2501_915918411 小时前
如何解析iOS崩溃日志:从获取到符号化分析
android·ios·小程序·https·uni-app·iphone·webview
一 乐3 小时前
学习辅导系统|数学辅导小程序|基于java+小程序的数学辅导小程序设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·学习·小程序
羊吖9 小时前
微信小程序图片上传系统性能优化实践
微信小程序·小程序
李慕婉学姐14 小时前
【开题答辩过程】以《“饭否”食材搭配指南小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring·小程序
00后程序员张16 小时前
Swift 应用加密工具的全面方案,从源码混淆到 IPA 成品加固的多层安全实践
安全·ios·小程序·uni-app·ssh·iphone·swift
低代码布道师18 小时前
医疗小程序11获取最近7天排班计划
低代码·小程序
2501_9160088919 小时前
提高 iOS 应用逆向难度的工程实践,多工具联动的全栈安全方案
android·安全·ios·小程序·uni-app·cocoa·iphone
云起SAAS19 小时前
早晚安打卡抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·早晚安打卡