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

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'
							}
						}
相关推荐
艾路菲尔27 分钟前
微信小程序地图缩放scale隐性bug
微信小程序
Good Lucky5 小时前
setData执行后操作方法-微信小程序
微信小程序·小程序·setdata
爱分享的程序员9 小时前
微信小程序执行C语言库的详细方案
c语言·微信小程序·小程序
Mark.10249 小时前
uniapp项目打包的微信小程序,设置uni-popup type=“bottom“时,底部有空隙
微信小程序·uni-app·notepad++
发呆小天才yy14 小时前
uniapp 微信小程序使用图表
前端·微信小程序·uni-app·echarts
Muxiyale17 小时前
微信小程序备案的一些记录
微信小程序·小程序
ThreeYear_s21 小时前
基于FPGA婴儿安全监护系统(蓝牙小程序监测)
fpga开发·小程序
天上掉下来个程小白1 天前
缓存菜品-04.功能测试
java·spring boot·缓存·微信小程序·需求分析·苍穹外卖
Mountain081 天前
微信小程序BLE蓝牙模块断开后无法再次搜索到原来的蓝牙
微信小程序·小程序