当前位置:网站首页>跨平台应用开发进阶(二十四) :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
}
});
}
}
})
五、拓展阅读
边栏推荐
- Skywalking 6.4 distributed link tracking usage notes
- Hidden rules of the workplace that must be understood before 30
- 重回榜首的大众,ID依然乏力
- 《性能之巅第2版》阅读笔记(五)--file-system监测
- 将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
- [advanced ROS] lesson 5 TF coordinate transformation in ROS
- Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization
- Implementation of wechat web page subscription message
- vim 从嫌弃到依赖(22)——自动补全
- Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
猜你喜欢

智能运维实战:银行业务流程及单笔交易追踪

Survey of intrusion detection systems:techniques, datasets and challenges

6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary

It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
![Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]](/img/ea/8c9f716717bc08f2e563c577738ec8.png)
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

S32K1xx 微控制器的硬件設計指南

【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测

Opencv Learning Notes 6 -- image mosaic

MySQL审计插件介绍
随机推荐
STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
Qt+pcl Chapter 9 point cloud reconstruction Series 2
Intelligent operation and maintenance practice: banking business process and single transaction tracking
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
Qt+pcl Chapter 6 point cloud registration ICP series 4
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
MySQL service is starting. MySQL service cannot be started. Solution
Qt+pcl Chapter 6 point cloud registration ICP series 3
OpenSSL client programming: SSL session failure caused by an insignificant function
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
榨汁机UL982测试项目有哪些
Opencv learning notes 5 -- document scanning +ocr character recognition
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
Reading notes of top performance version 2 (V) -- file system monitoring
[cloud trend] new wind direction in June! Cloud store hot list announced
入侵检测模型(An Intrusion-Detection Model)
SAP s/4hana: one code line, many choices
《QT+PCL第六章》点云配准icp系列6
Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching
The solution to turn the newly created XML file into a common file in idea