当前位置:网站首页>【下载文件】uniapp开发小程序,下载文件并保存到本地
【下载文件】uniapp开发小程序,下载文件并保存到本地
2022-07-28 02:23:00 【马小跳的Coding进阶之路】
一、效果图如下:
实现效果:点击文件附件,下载到本地
二、具体代码:
<view class="contact" @click="downloadFun(item.enclosure)">点击下载</view>
// 下载附件
downloadFun(enclosure){
var that = this
console.log('打印文件路径:',that.hostUrl + '/uploads'+ enclosure)
uni.downloadFile({
url: that.hostUrl + '/uploads'+ enclosure,//下载地址接口返回
success: (data) => {
console.log('打印data',data)
if (data.statusCode === 200) {
//文件保存到本地
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: function(res) {
uni.showToast({
icon: 'none',
mask: true,
// title: '文件已保存:' + res.savedFilePath, //保存路径
title: '文件下载成功' ,
duration: 3000,
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: res.savedFilePath,
success: function(res) {
// console.log('打开文档成功');
}
});
}, 3000)
}
});
}
},
fail: (err) => {
console.log(err);
uni.showToast({
icon: 'none',
mask: true,
title: '文件下载失败',
});
},
});
},
ending~
边栏推荐
- [email protected] Annotation usage
- CNN training cycle reconstruction - hyperparametric test | pytorch series (XXVIII)
- [acnoi2022] one step short
- Docker advanced -redis cluster configuration in docker container
- [red team] att & CK - file hiding
- mysql 随笔
- MySQL index learning
- MySQL essay
- What kind of job is social phobia suitable for? Can you do we media?
- Oracle basicfile lob field space recycling shrink space doubts
猜你喜欢

微服务架构统一安全认证设计与实践

Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!

Is it you who are not suitable for learning programming?

树莓派开发继电器控制灯

What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?

Web server

会议OA项目之我的审批&&签字功能

数据湖:数据库数据迁移工具Sqoop

Data Lake: database data migration tool sqoop

数据湖:海量日志采集引擎Flume
随机推荐
Where do I go to open an account for stock speculation? Is it safe to open an account on my mobile phone
Intelligent industrial design software company Tianfu C round financing of hundreds of millions of yuan
Redis群集
【stream】并行流与顺序流
openGauss源代码,用什么IDE工具管理、编辑、调试?
Promise object
NPDP考生!7月31号考试要求在这里看!
智能工业设计软件公司天洑C轮数亿元融资
数据中台建设(三):数据中台架构介绍
Embedded sharing collection 22
随机森林与集成方法学习笔记
一次跨域问题的记录
Es6.--promise, task queue and event cycle
Trivy [1] tool scanning application
QT专题1:实现一个简易计算器
[ACNOI2022]总差一步
ELS displays a random square
Design and practice of unified security authentication for microservice architecture
Record of a cross domain problem
【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法