1.下载文档
如下就可以下载成功了,但是,这就又发现了一个问题,下载好的文档不知道去哪里查找。
uni.downloadFile({
url: fileUrl,
success (res) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success (res) {
Toast('下载成功')
},
fail(error){
Toast('下载失败')
}
})
}
})
下面我们就换一种方式,使用 uni.openDocument() 直接将下载好的文件打开,就能找到位置了:
async handleLongpress() {
var fileType = this.format;
uni.downloadFile({
url: this.docUuidUrl,
success: function(res) {
var filePath = res.tempFilePath;
uni.openDocument({
filePath: filePath,
fileType: fileType,
success: function(res) {
console.log('打开文档成功');
},
});
}
});
},
文件类型记得要传。

2.下载视频
async handleLongpress() {
await uni.showLoading({
title: '下载中'
})
// 1. 将远程文件 下载到小程序内存中
const {
tempFilePath
} = (await uni.downloadFile({
url: this.vedioUuidUrl
}))[1]
// 2. 将内存中的文件 下载到本地系统相册
uni.saveVideoToPhotosAlbum({
filePath: tempFilePath
})
uni.hideLoading()
// 3. 提示用户下载成功
await uni.showToast({
title: '下载成功',
icon: 'none'
})
},
网友评论