当前位置:网站首页>跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
2022-07-01 15:33:00 【No Silver Bullet】
一、资源下载
APP应用开发过程中,资源下载是一种常见应用场景。uni-app中应用uni.downloadFile实现文件下载功能。示例代码如下:
downLoadFile() {
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保存到本地即可,实例代码如下:
downLoadFile() {
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”
三、资源打开
//文件保存到本地
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
}
});
}
}
})
五、拓展阅读
边栏推荐
- 微信小程序02-轮播图实现与图片点击跳转
- Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
- Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
- Qt+pcl Chapter 6 point cloud registration ICP Series 5
- 【一天学awk】函数与自定义函数
- Survey of intrusion detection systems:techniques, datasets and challenges
- Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization
- STM32ADC模拟/数字转换详解
- JS中箭头函数和普通函数的区别
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
猜你喜欢

【ROS进阶篇】第五讲 ROS中的TF坐标变换
SQL常用的四个排序函数梳理
![[one day learning awk] function and user-defined function](/img/e1/a378211ef05fcc4d469363f3e509a7.png)
[one day learning awk] function and user-defined function

MySQL审计插件介绍

MySQL高级篇4

JS中箭头函数和普通函数的区别

Returning to the top of the list, the ID is still weak
Sort out the four commonly used sorting functions in SQL

微信小程序02-轮播图实现与图片点击跳转

Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
随机推荐
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
Opencv learning note 4 -- bank card number recognition
《QT+PCL第六章》点云配准icp系列3
【显存优化】深度学习显存优化方法
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
Tableapi & SQL and MySQL grouping statistics of Flink
榨汁机UL982测试项目有哪些
skywalking 6.4 分布式链路跟踪 使用笔记
Wechat official account subscription message Wx open subscribe implementation and pit closure guide
6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
智能运维实战:银行业务流程及单笔交易追踪
微信小程序03-文字一左一右显示,行内块元素居中
说明 | 华为云云商店「商品推荐榜」
【天线】【3】CST一些快捷键
Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization
Implementation of deploying redis sentry in k8s
微信小程序02-轮播图实现与图片点击跳转
Flink 系例 之 TableAPI & SQL 与 Kafka 消息获取
What are the EN ISO 20957 certification standards for common fitness equipment