当前位置:网站首页>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
边栏推荐
- Photoshop插件-HDR(二)-脚本开发-PS插件
- 说明 | 华为云云商店「商品推荐榜」
- Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
- Tableapi & SQL and MySQL insert data of Flink
- Sort out the four commonly used sorting functions in SQL
- Can I choose to open an account on Great Wall Securities? Is it safe?
- [target tracking] |stark
- 【显存优化】深度学习显存优化方法
- STM32ADC模拟/数字转换详解
- Redis seckill demo
猜你喜欢
雷神科技冲刺北交所,拟募集资金5.4亿元
自动、智能、可视!深信服SSLO方案背后的八大设计
如何写出好代码 - 防御式编程指南
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
【显存优化】深度学习显存优化方法
微信小程序01-底部导航栏设置
[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)
Survey of intrusion detection systems:techniques, datasets and challenges
微信小程序03-文字一左一右显示,行内块元素居中
随机推荐
Tableapi & SQL and MySQL data query of Flink
HR面试:最常见的面试问题和技巧性答复
雷神科技冲刺北交所,拟募集资金5.4亿元
药品溯源夯实安全大堤
《QT+PCL第六章》点云配准icp系列2
【显存优化】深度学习显存优化方法
Description | Huawei cloud store "commodity recommendation list"
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
入侵检测模型(An Intrusion-Detection Model)
6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
[cloud trend] new wind direction in June! Cloud store hot list announced
Connect the ABAP on premises system to the central inspection system for custom code migration
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
What are the EN ISO 20957 certification standards for common fitness equipment
The difference between arrow function and ordinary function in JS
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
做空蔚来的灰熊,以“碰瓷”中概股为生?