wx.openDocument 和wx.downloadFile
wx.downloadFile({
url: app.globalData.ah_url + "url",
method:'GET',
success: function(res) {
console.log(res);
const tempFilePath = res.tempFilePath;
console.log(tempFilePath);
wx.openDocument({
filePath: tempFilePath,
fileType: 'xlsx',
success: function (res) {
console.log('打开文档成功')
},
fail: function(res) {
console.log("打开文档失败")
}
});
},
fail: function(res) {
console.log(res);
console.log('文件下载失败');
},
complete: function(res) {},
}) complete: function(res) {},
})
// "doc" | "docx" | "xls" | "xlsx" | "ppt" | "pptx" | "pdf" | undefined const types = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'] const fileType = types.find(i => fileName.endsWith(i)) as any console.log('文件预览:', url, fileName) wx.downloadFile({ // 示例 url,并非真实存在 url, filePath: wx.env.USER_DATA_PATH + `/${fileName}`, success(res) { if (res.statusCode === 200) { const filePath = res.filePath wx.showLoading({ title: '正在打开文件...' }) wx.openDocument({ filePath, fileType, success(result) { wx.hideLoading({}) console.log('打开文档成功', result) }, fail(res) { toast('文件打开失败') console.log('文件打开失败', res) }, complete(res) { toast('文件打开操作: ' + res.errMsg) console.log('文件打开操作', res) }, }) } }, fail() { toast('文件下载失败') }, })
希望有所帮助
是不是苹果手机上没有打开文件的工具,比如wps
一样情况
打开调试试下