53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
/**
|
|
* 预览文件的方法
|
|
* @param fileId 文件id
|
|
* @param fileName 文件名称
|
|
*/
|
|
|
|
// 文件下载的地址 后期上线需要修改地址
|
|
|
|
const downFileUrl = `http://localhost:3000/jero-boot/sys/common/download/`
|
|
// 预览
|
|
export function previewFile(fileId,fileName) {
|
|
|
|
// 目前没有在真机调试上试过 不知道要不要改前缀
|
|
// console.log(that.data.pdfFilePath.replace("wxfile", 'http'));
|
|
// 文件下载的地址
|
|
const url = downFileUrl + fileId
|
|
// 文件类型 目前是通过文件名称去截取
|
|
const fileType = sliceFileTypeByFileName(fileName)
|
|
// const fileType =
|
|
wx.downloadFile({
|
|
url: url,
|
|
success: function (res) {
|
|
wx.showToast({
|
|
title: '正在打开',
|
|
icon: 'loading',
|
|
mask: true,
|
|
duration: 1000
|
|
})
|
|
const filePath = res.tempFilePath
|
|
wx.openDocument({
|
|
filePath: filePath,
|
|
fileType: fileType,
|
|
success: function (res) {
|
|
console.log('打开文档成功')
|
|
wx.hideToast()
|
|
}
|
|
})
|
|
},
|
|
fail(err) {
|
|
console.log(err, '文件打开失败');
|
|
wx.hideToast()
|
|
}
|
|
})
|
|
}
|
|
|
|
// 截取文件类型的方法
|
|
function sliceFileTypeByFileName(fileName){
|
|
// 获取 . 的索引
|
|
let index = fileName.lastIndexOf('.')
|
|
// 截取文件类型
|
|
return fileName.slice(index+1)
|
|
}
|