uniapp微信小程序预览图片、文档、视频等格式文在这里插入代码片
onPreview(item) {
const url = item.link
const imageRegex = /(\.jpg|\.jpeg|\.png)$/i
const docRegex = /(\.doc|\.docx|\.pdf|\.ppt|\.xlsx|\.xls|\.pptx)$/i
const videoRegex = /(\.mp4|\.avi|\.mov|\.mkv|\.rmvb)$/i
const othersRegex = /(\.txt|\.csv)$/i
if (imageRegex.test(url)) {
uni.previewImage({
current: 0,
urls: [url]
})
} else if (docRegex.test(url)) {
this.lookFile(url)
} else if (videoRegex.test(url)) {
uni.navigateTo({
url: '/pages/webpageView/webpageView?src=' + encodeURIComponent(url)
})
} else if (othersRegex.test(url)) {
} else {
uni.$u.toast('该文件类型不支持预览!')
}
},
lookFile(url) {
uni.downloadFile({
url: url,
success: function(res) {
var filePath = res.tempFilePath
uni.openDocument({
filePath: filePath,
showMenu: true,
fileType: '',
success: function() {
console.log('打开文档成功')
},
fail: function() {
uni.showToast({
title: '打开失败',
icon: 'none'
})
}
})
}
})
},