微信小程序打开文档功能
html:
<view
style="display:flex;align-items:center;border:1rpx solid #bbbbbb;padding:10rpx;margin:10rpx;"
data-url="{{url}}"
bindtap="openFile">
<image
style="width:50rpx;height:50rpx;margin-right:10rpx;"
src="../../images/word.png"></image>
<view >IWMS虚拟仓储中心运营课程.doc</view>
</view>
js:
openFile(e){
console.log(e)
var url = e.currentTarget.dataset.url
wx.downloadFile({
url: url,
success:function(res){
var filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
fileType: 'pdf',
success(res){
console.log("打开成功")
},
fail(err){
console.log(err)
}
})
}
})
},
说明:获取到文件url,确保url可以打开,文件类型(fileType)需要设置。正常情况下一般可以打开文档。
真机调试如果打不开文档,可以尝试移除当前小程序,再重新调试即可。
注意:wx.openDocument仅支持doc、docx、xls、xlsx、ppt、pptx、pdf格式