// 阅读图书
readBook:function(e){
var that = this
// 获取当前点击图书的ID
let id = e.currentTarget.dataset.id
// 获取当前点击图书的Url
let fileUrl = e.currentTarget.dataset.file
// 查看本地缓存
let path =wx.getStorageSync(id)
// 未曾下载过
if(path ==''){
// 切换到下载时的蒙层
that.setData({
isDownloading:true
})
// 先下载图书
const downloadTask = wx.downloadFile({
url:fileUrl,
success:function(res){
// 关闭下载时的蒙层
that.setData({
isDownloading:false
})
// 下载成功
if (res.statusCode == 200){
// 获取地址
path = res.tempFilePath
// 保存并打开图书
that.saveBook(id,path)
}
// 连上服务器,下载失败
else{
that.showTips('暂时无法下载')
}
},
//请求失败
fail:function(e){
// 关闭下载时的蒙层
that.setData({
isDownloading:false
})
that.showTips('无法连接到服务器!')
}
})
// 监听当前文件下载的下载速度
downloadTask.onProgressUpdate((res) =>{
let progress = res.progress
that.setData({
percentNum: progress
})
})
}
else{
// 打开图书
that.openBook(path)
}
},
当我点击图书下载前,无错误,但当我点击时,只会提示弹出‘无法连接到服务器’这个消息框,但是无法下载,并出错以下错误
thirdScriptError
Cannot read property ‘onProgressUpdate’ of undefined; [Component] Event Handler Error @ pages/index/index#bound readBook
TypeError: Cannot read property ‘onProgressUpdate’ of undefined
at me.readBook (http://127.0.0.1:52924/appservice/pages/index/index.js:141:18)
at Object.r.safeCallback (http://127.0.0.1:52924/appservice/__dev__/WAService.js:2:1434440)
at http://127.0.0.1:52924/appservice/__dev__/WAService.js:2:1552253
at s (http://127.0.0.1:52924/appservice/__dev__/WAService.js:2:1561021)
at http://127.0.0.1:52924/appservice/__dev__/WAService.js:2:1552178
at r (http://127.0.0.1:52924/appservice/__dev__/WAService.js:2:1505702)
当我点击图书时,只提示‘无法连接到服务器’这个选项,但是无法下载,
’‘