当前位置:网站首页>Advanced cross platform application development (24): uni app realizes file download and saving
Advanced cross platform application development (24): uni app realizes file download and saving
2022-07-01 15:40:00 【No Silver Bullet】
List of articles
One 、 Download resources
APP In the process of application development , Resource downloading is a common application scenario .uni-app
Application in uni.downloadFile
Realize file download function . The sample code is as follows :
downLoadFile() {
const downloadTask = uni.downloadFile({
url: 'http://img.netbian.com/file/2019/0414/7bee7eef5fc44417a0b02a46576e7e16.jpg', // Just for the sample , Not real resources
success: (res) => {
if (res.statusCode === 200) {
console.log(' Download successful ');
}
this.dd = res.tempFilePath;
console.log(this.dd);
}
});
downloadTask.onProgressUpdate((res) => {
console.log(' Download progress ' + res.progress);
console.log(' The length of downloaded data ' + res.totalBytesWritten);
console.log(' The total length of data that is expected to be downloaded ' + res.totalBytesExpectedToWrite);
});
}
notes : The temporary path to the file , It can be used normally during the application startup , For long-term preservation , Need to call on the initiative uni.saveFile
, Can be accessed the next time the application starts .
Two 、 Resource preservation
When an application uni.downloadFile
After the callback is successful tempFilePath
The parameter represents the path to temporarily save the file , Reuse uni.saveFile
Save it locally , The example code is as follows :
downLoadFile() {
const downloadTask = uni.downloadFile({
url: 'http://img.netbian.com/file/2019/0414/7bee7eef5fc44417a0b02a46576e7e16.jpg', // Just for the sample , Not real resources
success: (res) => {
if (res.statusCode === 200) {
console.log(' Download successful ');
}
let that = this;
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(red) {
that.luj = red.savedFilePath
console.log(red)
}
});
}
});
downloadTask.onProgressUpdate((res) => {
console.log(' Download progress ' + res.progress);
console.log(' The length of downloaded data ' + res.totalBytesWritten);
console.log(' The total length of data that is expected to be downloaded ' + res.totalBytesExpectedToWrite);
});
}
The location where resources are downloaded and saved is :
“ Internal storage \Android\data\io.dcloud.HBuilder\apps\HBuilder\doc\uniapp_save”
3、 ... and 、 Resource open
// Save the file locally
uni.saveFile({
tempFilePath: data.tempFilePath, // Temporary path
success: function(res) {
uni.showToast({
icon: 'none',
mask: true,
title: ' The file has been saved :' + res.savedFilePath, // Save the path
duration: 3000,
});
setTimeout(() => {
// Open document view
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
// console.log(' Successfully opened the document ');
}
});
}, 3000)
}
});
Four 、 Save pictures to your local album
uni.downloadFile({
url: imgUrl[0],
success: (res) => {
if (res.statusCode === 200) {
// Save picture to system album
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
uni.showToast({
title: " Saved successfully ",
icon: "none"
});
return
},
fail: function() {
uni.showToast({
title: " Save failed , Please try again later ",
icon: "none"
});
return
}
});
}
}
})
5、 ... and 、 Expanding reading
边栏推荐
- 张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
- Reading notes of top performance version 2 (V) -- file system monitoring
- Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
- The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
- vim 从嫌弃到依赖(22)——自动补全
- [advanced ROS] lesson 5 TF coordinate transformation in ROS
- RT-Thread Env 工具介绍(学习笔记)
- What are the test items of juicer ul982
- MySQL advanced 4
- Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
猜你喜欢
MySQL advanced 4
Implementation of wechat web page subscription message
点云重建方法汇总一(PCL-CGAL)
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
How to realize clock signal frequency division?
SQL常用的四个排序函数梳理
STM32ADC模拟/数字转换详解
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
【一天学awk】函数与自定义函数
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
随机推荐
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
Tableapi & SQL and MySQL insert data of Flink
Équipe tensflow: Nous ne sommes pas abandonnés
Tableapi & SQL and MySQL data query of Flink
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
MySQL service is starting. MySQL service cannot be started. Solution
SAP s/4hana: one code line, many choices
Wechat official account subscription message Wx open subscribe implementation and pit closure guide
Summary of point cloud reconstruction methods I (pcl-cgal)
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
Tensorflow team: we haven't been abandoned
Preorder, inorder, follow-up of binary tree (non recursive version)
TensorFlow團隊:我們沒被拋弃
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
《QT+PCL第六章》点云配准icp系列3
Summary of week 22-06-26
Deep operator overloading (2)