微信小程序查看附件
通过判断使用wx.previewImage来查看图片,
wx.previewMedia来查看视频
wx.downloadFile+wx.saveFile+ wx.openDocument来查看文档,例如word 、pdf
使用看一下详情
lookfile(e) {
const url = e.currentTarget.dataset.url;
let index = url.lastIndexOf('.')
let filttype = url.slice(index + 1)
wx.showLoading({
title: '加载中',
mask: true
})
if (['bmp', 'jpg', 'jpeg', 'png', 'gif', 'image'].some(item => item == filttype)) {
wx.previewImage({
current: url,
urls: [url],
success() {
wx.hideLoading()
}
})
} else if (['mp4'].some(item => item == filttype)) {
wx.previewMedia({
sources: [
{
url: e.currentTarget.dataset.url,
type: 'video'
}
],
current: 1,
success(){
wx.hideLoading()
}
})
} else if (['zip', 'rar'].some(item => item == filttype)) {
wx.showToast({
title: '不好意思,暂不支持预览,请到pc端查看',
icon:"none"
})
} else {
wx.downloadFile({
url: e.currentTarget.dataset.url,
header: {
"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
},
success: function (res) {
console.log(res)
const tempFilePath = res.tempFilePath;
wx.saveFile({
tempFilePath,
success(res) {
console.log(res)
const savedFilePath = res.savedFilePath;
wx.openDocument({
filePath: savedFilePath,
showMenu: true,
flieType: filttype,
success: function (res) {
wx.hideLoading()
console.log('打开文档成功')
},
fail: function (err) {
wx.hideLoading()
wx.showToast({
title: '不好意思,暂不支持预览,请到pc端查看',
icon:"none"
})
}
});
},
})
},
fail: function (err) {
wx.showToast({
title: '下载失败',
})
console.log(err)
}
})
}
},