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

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'
							}
						}
相关推荐
三原11 小时前
7000块帮朋友做了2个小程序加一个后台管理系统,值不值?
前端·vue.js·微信小程序
Byte_Me12 小时前
IoT 小程序:如何破解设备互联的碎片化困局?
物联网·小程序·iot
小徐_233314 小时前
uni-app 弹窗总被父元素“绑架”?3招破局,H5/小程序/APP一招通杀!
前端·微信小程序·uni-app
難釋懷19 小时前
微信小程序案例 - 本地生活(首页)
微信小程序·生活·notepad++
2501_915921431 天前
没有Mac如何完成iOS 上架:iOS App 上架App Store流程
android·ios·小程序·https·uni-app·iphone·webview
说私域1 天前
基于定制开发开源AI智能名片与S2B2C商城小程序的旅游日志创新应用研究
人工智能·小程序·旅游
脑袋大大的1 天前
钉钉小程序开发环境配置与前端开发指南
小程序·钉钉·企业应用开发
2301_805962932 天前
微信小程序控制空调之接收MQTT消息
微信小程序·小程序·esp32
The_era_achievs_hero2 天前
微信小程序121~130
微信小程序·小程序
難釋懷2 天前
微信小程序WXSS 模板样式
微信小程序·小程序·notepad++