- uni.downloadFile下载
xiazai() {
const downloadTask = uni.downloadFile({
url: 'http://img.netbian.com/file/2019/0414/7bee7eef5fc44417a0b02a46576e7e16.jpg', //仅为示例,并非真实的资源
success: (res) => {
if (res.statusCode === 200) {
console.log('下载成功');
}
this.dd = res.tempFilePath;
console.log(this.dd);
}
});
downloadTask.onProgressUpdate((res) => {
console.log('下载进度' + res.progress);
console.log('已经下载的数据长度' + res.totalBytesWritten);
console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
});
}
- uni.saveFile永久保存
当uni.downloadFile 回调成功后tempFilePath参数临时保存文件的路径,在使用uni.saveFile存到本地即可,整体代码如下
xiazai() {
const downloadTask = uni.downloadFile({
url: 'http://img.netbian.com/file/2019/0414/7bee7eef5fc44417a0b02a46576e7e16.jpg', //仅为示例,并非真实的资源
success: (res) => {
if (res.statusCode === 200) {
console.log('下载成功');
}
let that = this;
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(red) {
that.luj = red.savedFilePath
console.log(red)
}
});
}
});
downloadTask.onProgressUpdate((res) => {
console.log('下载进度' + res.progress);
console.log('已经下载的数据长度' + res.totalBytesWritten);
console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
});
}
- 保存位置
它保存的位置比奇葩在:
“内部存储\Android\data\io.dcloud.HBuilder\apps\HBuilder\doc\uniapp_save”
“内部存储\Android\data\io.dcloud.HBuilder\apps\HBuilder\doc\uniapp_save”
- 保存后打开
//文件保存到本地
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: function(res) {
uni.showToast({
icon: 'none',
mask: true,
title: '文件已保存:' + res.savedFilePath, //保存路径
duration: 3000,
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
// console.log('打开文档成功');
}
});
}, 3000)
}
});
- 图片保存到本机相册
uni.downloadFile({
url: imgUrl[0],
success: (res) => {
if (res.statusCode === 200) {
//保存图片到系统相册
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
uni.showToast({
title: "保存成功",
icon: "none"
});
return
},
fail: function() {
uni.showToast({
title: "保存失败,请稍后重试",
icon: "none"
});
return
}
});
}
}
})
标签:saveFile,console,log,success,res,app,保存,uni
From: https://www.cnblogs.com/linhan8888/p/17005774.html