查看: 858|回复: 0

uniapp开发移动端下载文件到本地

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2025-2-5 16:18:34 | 显示全部楼层 |阅读模式
代码:

  1. uni.downloadFile({
  2.                                         url: this.article.fileUrl,
  3.                                         success(res) {
  4.                                                 console.log(res)
  5.                                                 if (res.statusCode == 200) {
  6.                                                         let filePath = res.tempFilePath; // 下载临时路径
  7.                                                         // #ifdef APP || MP-BAIDU || MP-ALIPAY || MP-TOUTIAO || MP-XHS
  8.                                                         uni.saveFile({
  9.                                                                 tempFilePath: filePath,
  10.                                                                 success(t) {
  11.                                                                         console.log(t)
  12.                                                                         uni.showModal({
  13.                                                                                 title: '提示',
  14.                                                                                 content: '文件存储位置:'+t.savedFilePath,
  15.                                                                                 confirmText: '复制位置',
  16.                                                                                 success(e) {
  17.                                                                                         if (e.confirm) {
  18.                                                                                                 uni.setClipboardData({
  19.                                                                                                         data: t.savedFilePath,
  20.                                                                                                         success() {
  21.                                                                                                                 uni.showToast({
  22.                                                                                                                         title: '复制成功',
  23.                                                                                                                         icon: 'none'
  24.                                                                                                                 })
  25.                                                                                                         },
  26.                                                                                                         fail() {
  27.                                                                                                                 uni.showToast({
  28.                                                                                                                         title: '复制失败'
  29.                                                                                                                 })
  30.                                                                                                         }
  31.                                                                                                 })
  32.                                                                                         }
  33.                                                                                 }
  34.                                                                         })
  35.                                                                 },
  36.                                                                 fail() {
  37.                                                                         uni.showModal({
  38.                                                                                 title: '提示',
  39.                                                                                 content: '文件保存失败'
  40.                                                                         })
  41.                                                                 }
  42.                                                         })
  43.                                                         // #endif
  44.                                                         // #ifdef MP-WEIXIN
  45.                                                         let FileSystemManager = uni.getFileSystemManager();
  46.                                                         FileSystemManager.saveFile({
  47.                                                                 tempFilePath: filePath,
  48.                                                                 success(t) {
  49.                                                                         console.log(t)
  50.                                                                         uni.showModal({
  51.                                                                                 title: '提示',
  52.                                                                                 content: '文件存储位置:'+t.savedFilePath,
  53.                                                                                 confirmText: '复制位置',
  54.                                                                                 success(e) {
  55.                                                                                         if (e.confirm) {
  56.                                                                                                 uni.setClipboardData({
  57.                                                                                                         data: t.savedFilePath,
  58.                                                                                                         success() {
  59.                                                                                                                 uni.showToast({
  60.                                                                                                                         title: '复制成功',
  61.                                                                                                                         icon: 'none'
  62.                                                                                                                 })
  63.                                                                                                         },
  64.                                                                                                         fail() {
  65.                                                                                                                 uni.showToast({
  66.                                                                                                                         title: '复制失败'
  67.                                                                                                                 })
  68.                                                                                                         }
  69.                                                                                                 })
  70.                                                                                         }
  71.                                                                                 }
  72.                                                                         })
  73.                                                                 },
  74.                                                                 fail() {
  75.                                                                         uni.showModal({
  76.                                                                                 title: '提示',
  77.                                                                                 content: '文件保存失败'
  78.                                                                         })
  79.                                                                 }
  80.                                                         })
  81.                                                         // #endif
  82.                                                 }
  83.                                         }
  84.                                 })
复制代码
app端:
  1. saveFileToLocal(tempFilePath) {
  2.                             // 获取存储目录
  3.                             let fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
  4.                             let savePath = plus.io.convertLocalFileSystemURL(`_downloads/${fileName}`); // 目标存储路径
  5.                        
  6.                             plus.io.resolveLocalFileSystemURL('_downloads/', (entry) => {
  7.                                 entry.getFile(fileName, { create: true }, (fileEntry) => {
  8.                                     fileEntry.createWriter((writer) => {
  9.                                         writer.write(tempFilePath);
  10.                                         console.log("文件保存成功:", savePath);
  11.                                     }, (e) => console.error("写入失败", e));
  12.                                 });
  13.                             });
  14.                         }
复制代码


道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部