当前位置:网站首页>跨平台应用开发进阶(二十四) :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
}
});
}
}
})
五、拓展阅读
边栏推荐
- Hardware design guide for s32k1xx microcontroller
- Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
- [target tracking] |stark
- MySQL审计插件介绍
- S32K1xx 微控制器的硬件設計指南
- 异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
- [leetcode] 16. The sum of the nearest three numbers
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- Can I choose to open an account on Great Wall Securities? Is it safe?
猜你喜欢
SQL常用的四个排序函数梳理
《QT+PCL第九章》点云重建系列2
Qt+pcl Chapter 9 point cloud reconstruction Series 2
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
leetcode:329. Longest increasing path in matrix
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
Fix the failure of idea global search shortcut (ctrl+shift+f)
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Sort out the four commonly used sorting functions in SQL
随机推荐
【LeetCode】16、最接近的三数之和
Opencv learning notes 5 -- document scanning +ocr character recognition
MySQL审计插件介绍
Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization
MySQL 服务正在启动 MySQL 服务无法启动解决途径
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
phpcms后台上传图片按钮无法点击
Wechat official account subscription message Wx open subscribe implementation and pit closure guide
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
Summary of week 22-06-26
【锁】Redis锁 处理并发 原子性
【一天学awk】函数与自定义函数
Phpcms background upload picture button cannot be clicked
说明 | 华为云云商店「商品推荐榜」
选择在长城证券上炒股开户可以吗?安全吗?
Introduction to MySQL audit plug-in
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
榨汁机UL982测试项目有哪些
Stm32f4-tft-spi timing logic analyzer commissioning record