微信小程序中文件的预览下载,文件分享
// 预览
const downupload = (id) => {
uni.showLoading({
title: '请稍等...',
mask: true,
})
//打开预览展示文件名称,
let fileN = id == 1 ? '表格名称.docx' : id == 2 ? '表格名称.docx' : id == 3 ?
'表格名称.docx' : ''
let filePath = `${wx.env.USER_DATA_PATH}/${fileN}`
const downloadTask = wx.downloadFile({
url: `文件链接`,//后端提供
filePath: filePath,
success: (res) => {
if (res.statusCode == 200) {
// 预览
var Path = res.filePath
uni.openDocument({
// filePath: res.tempFilePath,
filePath: Path,
showMenu: true,
success(res) {
console.log('成功打开文档');
uni.hideLoading();//关闭showLoading
}
})
}
},
fail: function(res) {
console.log('下载异常:' + res.errMsg)
},
})
}
// 分享
const shareFn = (val) => {
wx.downloadFile({
url: `文件链接`,
success(res) {
//隐藏加载框
uni.hideLoading();
if (res.statusCode == 200) {
wx.shareFileMessage({
filePath: res.tempFilePath,
success(data) {
uni.showToast({
icon: 'success',
mask: true,
duration: 2000,
});
},
fileName: val == 1 ? '表格名称.docx' : val == 2?'表格名称.docx' :val == 3 ?'表格名称.docx' : '',
fail: console.error,
})
}
},
})
}