当前位置:网站首页>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
边栏推荐
- Wechat official account subscription message Wx open subscribe implementation and pit closure guide
- Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
- 七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
- 软件测试的可持续发展,必须要学会敲代码?
- MySQL审计插件介绍
- 精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
- MySQL backup and restore single database and single table
- 远程办公经验?来一场自问自答的介绍吧~ | 社区征文
- 她就是那个「别人家的HR」|ONES 人物
- go-zero实战demo(一)
猜你喜欢
Implementation of deploying redis sentry in k8s

张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例

STM32F4-TFT-SPI时序逻辑分析仪调试记录

Introduction to MySQL audit plug-in

软件测试的可持续发展,必须要学会敲代码?

"Qt+pcl Chapter 6" point cloud registration ICP Series 6

Qt+pcl Chapter 9 point cloud reconstruction Series 2

Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?

《性能之巅第2版》阅读笔记(五)--file-system监测

Survey of intrusion detection systems:techniques, datasets and challenges
随机推荐
TS reports an error don't use 'object' as a type The `object` type is currently hard to use
An intrusion detection model
MySQL service is starting. MySQL service cannot be started. Solution
摩根大通期货开户安全吗?摩根大通期货公司开户方法是什么?
求求你们,别再刷 Star 了!这跟“爱国”没关系!
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
Summary of week 22-06-26
工厂高精度定位管理系统,数字化安全生产管理
What are the EN ISO 20957 certification standards for common fitness equipment
Connect the ABAP on premises system to the central inspection system for custom code migration
三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
[video memory optimization] deep learning video memory optimization method
有些能力,是工作中学不来的,看看这篇超过90%同行
TensorFlow團隊:我們沒被拋弃
SAP S/4HANA: 一条代码线,许多种选择
Photoshop插件-HDR(二)-脚本开发-PS插件
做空蔚来的灰熊,以“碰瓷”中概股为生?
swiper 轮播图,最后一张图与第一张图无缝衔接
说明 | 华为云云商店「商品推荐榜」
她就是那个「别人家的HR」|ONES 人物