
- 发布:2021-01-26 13:12
- 更新:2021-01-26 13:12
- 阅读:1300
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: w10
HBuilderX类型: 正式
HBuilderX版本号: 3.0.7
手机系统: 全部
手机厂商: 苹果
页面类型: vue
打包方式: 离线
项目创建方式: HBuilderX
测试过的手机:
示例代码:
download(row) {
this.downloadTask = ""
this.countkb = 0
this.presentkb = 0
this.percent = 0
this.show = !this.show
this.downloadTask = uni.downloadFile({
url: http://xxx/download?token=${this.token}&cellname=${row.cellname}&filename=${row.name}
,
success: (data) => {
if (data.statusCode === 200 && data.tempFilePath) {
this.$refs.uToast.show({
title: '下载成功',
type: 'success'
})
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: res => {
setTimeout(() => {
this.show = false
}, 2800)
uni.showToast({
icon: "none",
mask: true,
title: '文件已保存:' + res.savedFilePath, //保存路径
duration: 2000,
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: res.savedFilePath,
success: resd => {
console.log('打开文档成功');
}
});
}, 3000)
}
});
}
},
complete: () => {}
});
if (this.downloadTask) {
this.jt()
}
},
jt() {
this.downloadTask.onProgressUpdate(resda => {
console.log(resda)
console.log('下载进度' + resda.progress);
console.log('已经下载的数据长度' + resda.totalBytesWritten);
console.log('预期需要下载的数据总长度' + resda.totalBytesExpectedToWrite);
this.percent = resda.progress
this.presentkb = (resda.totalBytesWritten / 1024).toFixed(2)
this.countkb = (resda.totalBytesExpectedToWrite / 1024).toFixed(2)
})
},
download(row) {
this.downloadTask = ""
this.countkb = 0
this.presentkb = 0
this.percent = 0
this.show = !this.show
this.downloadTask = uni.downloadFile({
url: http://xxx/download?token=${this.token}&cellname=${row.cellname}&filename=${row.name}
,
success: (data) => {
if (data.statusCode === 200 && data.tempFilePath) {
this.$refs.uToast.show({
title: '下载成功',
type: 'success'
})
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: res => {
setTimeout(() => {
this.show = false
}, 2800)
uni.showToast({
icon: "none",
mask: true,
title: '文件已保存:' + res.savedFilePath, //保存路径
duration: 2000,
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: res.savedFilePath,
success: resd => {
console.log('打开文档成功');
}
});
}, 3000)
}
});
}
},
complete: () => {}
});
if (this.downloadTask) {
this.jt()
}
},
jt() {
this.downloadTask.onProgressUpdate(resda => {
console.log(resda)
console.log('下载进度' + resda.progress);
console.log('已经下载的数据长度' + resda.totalBytesWritten);
console.log('预期需要下载的数据总长度' + resda.totalBytesExpectedToWrite);
this.percent = resda.progress
this.presentkb = (resda.totalBytesWritten / 1024).toFixed(2)
this.countkb = (resda.totalBytesExpectedToWrite / 1024).toFixed(2)
})
},
操作步骤:
点击下载。使用uniapp下载api,使用监听api,返回监听数据给手机。
点击下载。使用uniapp下载api,使用监听api,返回监听数据给手机。
预期结果:
正常返回监听结果,console.log('下载进度' + resda.progress);
console.log('已经下载的数据长度' + resda.totalBytesWritten);
console.log('预期需要下载的数据总长度' + resda.totalBytesExpectedToWrite
正常返回监听结果,console.log('下载进度' + resda.progress);
console.log('已经下载的数据长度' + resda.totalBytesWritten);
console.log('预期需要下载的数据总长度' + resda.totalBytesExpectedToWrite
实际结果:
console.log('下载进度' + resda.progress);
console.log('已经下载的数据长度' + resda.totalBytesWritten);
console.log('预期需要下载的数据总长度' + resda.totalBytesExpectedToWrite);
没有返回
console.log('下载进度' + resda.progress);
console.log('已经下载的数据长度' + resda.totalBytesWritten);
console.log('预期需要下载的数据总长度' + resda.totalBytesExpectedToWrite);
没有返回
bug描述:
uni.downloadFile(OBJECT)下载方法中,onProgressUpdate callback 监听下载进度变化,
downloadTask.onProgressUpdate((res) => {
console.log('下载进度' + res.progress);
console.log('已经下载的数据长度' + res.totalBytesWritten);
console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
});
Android返回监听结果一直没有数据,ios下载完成有一次正常返回结果。
监听完全没有用
0 个回复