小程序中预览文件时在手机端打开是正常的,PC端打开后文件内容就显示不全,并不所有文件都这样,相同的文件格式下部分文件会出现这种情况
文件地址:https://file.51xinyoushi.cn/teaching_resource/1/G4OAEbyp-2A Unit 1 单元教学设计.docx
这是电脑端预览打开的效果,只显示出了一页,实际是有十多页的:
这是手机端打开的效果,整个文件的内容都能正常完整显示:
代码:
const file = {
fileName: "2A Unit 1 单元教学设计.docx",
fileUrl: "https://file.51xinyoushi.cn/teaching_resource/1/G4OAEbyp-2A Unit 1 单元教学设计.docx",
}
wx.downloadFile({
url: file.fileUrl,
filePath: wx.env.USER_DATA_PATH + '/' + file.fileName,
success: (res) => {
wx.hideLoading();
if (res.statusCode === 200) {
wx.openDocument({
filePath: res.filePath,
showMenu: true,
success: () => {
console.log('打开文档成功');
},
fail: (err) => {
console.error('打开文档失败:', err);
wx.showToast({
title: '无法打开此文件',
icon: 'none'
});
}
});
} else {
wx.showToast({
title: '文件下载失败',
icon: 'none'
});
}
},
fail: (err) => {
wx.hideLoading();
console.error('下载文件失败:', err);
wx.showToast({
title: '下载失败,请重试',
icon: 'none'
});
}
});
