微信小程序预览文件 兼容性苹果

javascript 复制代码
uni.request({
					url: url,
					method: 'GET',
					header: {
						'Authorization': token,
						responseType: 'blob',
					},
					responseType: "arraybuffer",
					success: (res) => {
						uni.hideLoading()
						const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
						let index = url.lastIndexOf(".");
						const typeRp = url.slice(index, url.length);
						let fileType = url.slice(index + 1, url.length);
						var platform = wx.getSystemInfoSync();
						if (platform.platform === "ios") {
							if (fileType == 'xls') {
								fileType = fileType + 'x'
							}
						}
						const filePath = `${wx.env.USER_DATA_PATH}/preview${typeRp}`;
						fs.writeFile({
							filePath,
							data: res.data,
							encoding: "binary", //二进制流文件必须是 binary
							success(res) {
								uni.hideLoading()
								// console.log(res, '写入成功', typeRp, fileType);
								fs.access({
									path: filePath,
									success: () => {
										wx.openDocument({
											filePath,
											fileType,
											showMenu: true,
											success: function(res) {
												setTimeout(() => {
													wx.hideLoading();
												}, 500);
											},
										});
									},
									fail: (err) => {
										uni.hideLoading()
									},
								});
							},
						});
					},
					fail: (err) => {
						uni.hideLoading()
					},
				})

如果是苹果系统就需要修改一下后缀

javascript 复制代码
var platform = wx.getSystemInfoSync();
						if (platform.platform === "ios") {
							if(fileType == 'xls') {
								fileType = fileType + 'x'
							}
						}
相关推荐
qq_433502185 小时前
微信小程序更新机制踩坑记录:updateInfo 为什么总是读到旧数据?
微信小程序·小程序·notepad++
优雅的大白鹅21 小时前
创建uniapp小程序
小程序·uni-app
liangdabiao1 天前
开源AI拼豆大升级 - 一键部署cloudflare page - 全免费 web和小程序
前端·人工智能·小程序
px不是xp1 天前
DeepSeek API集成:让小程序拥有AI大脑
javascript·人工智能·小程序
小汪说干货1 天前
2026年4月最新|公众号文章插入文档附件3种技术方案
javascript·小程序
QQ22792391021 天前
Java springboot基于微信小程序的智慧旅游导游系统景点门票酒店预订(源码+文档+运行视频+讲解视频)
java·spring boot·微信小程序·maven·vuejs
小汪说干货1 天前
2026年4月优化版|公众号文档格式附件插入3种方案(推荐“附链”小程序)
小程序·微信公众平台
万岳科技系统开发2 天前
商城系统搭建自建平台与入驻第三方平台对比分析
数据库·小程序·架构
笨笨狗吞噬者2 天前
uni-app 运行时揭秘:styleIsolation 的转化
前端·微信小程序·uni-app
double_eggm2 天前
微信小程序3
微信小程序·小程序