小程序文件批量下载保存

小程序批量下载文件到本地用户文件夹

由于小程序大小2M限制所以用到的音频图片啥的可以放到第三方平台百度网盘或小程序云储存,然后在用户首次使用时把资源下载到本地,以后启动时检查是否存在此文件不在就下载,理论不卸载小程序文件一直在,空间为10M

封装批量下载js

把下面代码复制后新建一个js,命名为
downloadfile.js把代码添加进去,

 function download(obj) {
  let success = obj.success; //下载成功
  let fail = obj.fail; //下载失败
  let ml = obj.ml //目录
  let data = obj.data //数据
  let split = obj.split;   //你自己的链接部分
  let manager = wx.getFileSystemManager();  //获取全局唯一的文件管理器
  //判断目录是否存在
  manager.access({
    path: `${wx.env.USER_DATA_PATH}/${ml}`,
    success: (res) => {
      // console.log('已存在path对应目录',res)
      //保存文件之前查看是否存在此文件  
      let c=0
      let down = wx.getStorageSync(ml)
      for (let item of data) { 
        let filename = item.split(split)[1];
        manager.access({
          path: `${wx.env.USER_DATA_PATH}/${ml}/${filename}`,
          success(res) {
            c=c+1
            console.log('已存在此文件', res,c);
            //return false;
            if(c==data.length){
              console.log('下载完毕')
              success(down);
            }
          },
          fail(err) {
            console.log('不存在此文件')
            wx.downloadFile({
              url: item,
              filePath: `${wx.env.USER_DATA_PATH}/${ml}/${filename}`, //设置路径,并且设置文件名为${filename}
              success: function (res) {
                c = c + 1
                console.log('下载单个成功', res, item,c)
                for(let i of down){
                  if(i.item==item){
                    i.filePath=res.filePath
                  }else{
                    let filePath = res.filePath
                    down.push({ item, filePath })
                  }
                }
                wx.setStorageSync(ml, down)
                if (c == data.length) {
                  console.log('下载完毕')
                  success(down);
                }
              },
              fail: function (e) {
                console.info("下载一个文件失败", item, e);
                if (fail) {
                  fail(e);
                }
              }
            })

          }
        })
      }
    },
    fail: (err) => {
      // console.log(err, '不存在path对应目录')
      //创建保存文件的目录
      manager.mkdir({
        dirPath: `${wx.env.USER_DATA_PATH}/${ml}`,
        recursive: false,
        success: (res) => {
          //将临时文件保存到目录  /${ml}
          let down = []
          for (let item of data) {
            let filename = item.split(split)[1];
            wx.downloadFile({
              url: item,
              filePath: `${wx.env.USER_DATA_PATH}/${ml}/${filename}`, //设置路径,并且设置文件名为${filename}
              success: function (res) {
                console.log('下载成功', res, item)
                let filePath = res.filePath
                down.push({ item, filePath })
                if (down.length == data.length) {
                  wx.setStorageSync(ml, down)
                  success(down);
                }
              },
              fail: function (e) {
                console.info("下载一个文件失败", item, e);
                if (fail) {
                  fail(e);
                }
              }
            })
          }
        },
        fail: (err) => {
          console.log('文件夹创建失败', err)
          if (fail) {
            fail(err);
          }
        }
      })
    }
  })
}
module.exports = {
  download: download
}

在需要使用的界面js中加入下面代码

import download from "../../utils/downloadfile.js"//你刚才创建的那个downloadfile.js路径
data:{
mp3: ['https://tcb.qcloud.la/audiod/10seconds.mp3', 'https://tcb.qcloud.la/audiod/allclose.mp3', 'https://tcb.qcloud.la/audiod/allopen.mp3']
},
//下载资源
downloadmp3() {
return new Promise((resolve, reject) => {
download.download({
data:this.data.mp3,
ml: 'mp3',//目录名字
split: 'https://tcb.qcloud.la/audiod/',//截取你网址的前面部分留后面的名字
success: function (res) {
console.log('下载mp3成功返回的', res)
resolve(res)
},
fail: function (e) {
wx.hideLoading();
wx.showToast({
title: 'MP3资源下载失败',
icon: none,
duration: 2000
})
console.info("下载mp3失败", e);
reject(e)
}
});})
},
在给你一个检测的代码检测目前下载了那些文件和删除的代码自己分离下,下面代码建议单独建立一个test页
test2(){
//获取缓存文件删除
wx.getSavedFileList({
success(res) {
console.log(res)
if (res.fileList.length > 0) {
for (let i = 0; i < res.fileList.length; i++){
wx.removeSavedFile({
filePath: res.fileList[i].filePath,
complete(res) {
console.log('删除缓存',res)
}
})
}
}
}
})
//读取用户文件删除
FileSystemManager.readdir({
dirPath: `${wx.env.USER_DATA_PATH}/mp3/`,
success: function (res) {
console.log('res系统文件管理', res)
for (let item of res.files) {
//除miniprogramLog这个文件不删
if (item != 'miniprogramLog') {
FileSystemManager.unlink({
filePath: `${wx.env.USER_DATA_PATH}/mp3/${item}`,//删除路径mp3目录
success: function (res) {
console.log('res系统文件管理删除成功', res)
},
fail: (err) => {
console.log('res系统文件管理删除失败: ', err)
}
})
}
}
},
fail: (err) => {
console.log('本地文件列表读取失败: ', err)
}
})
},


//读取本地文件
getsavedinfo() {
var countsize=0
FileSystemManager.readdir({
dirPath: `${wx.env.USER_DATA_PATH}/mp3/`,
success: function(res){
console.log('res系统文件管理',res)
for(let item of res.files){
if (item != 'miniprogramLog'){
FileSystemManager.getFileInfo({
filePath: `${wx.env.USER_DATA_PATH}/mp3/${item}`,
success: function (re) {
countsize+=re.size/1024
if (item == res.files[res.files.length-1]){
console.log('用户文件总大小' + countsize + 'kb')
}
},
fail: (err) => {
console.log('查询失败: ', err)
}
})
}
}
},
fail: (err) => {
console.log('本地文件列表读取失败: ',err)}
})

//读取缓存文件
wx.getSavedFileList({
success(res) {
console.log(res)
var size=0
for (let item of res.fileList){
size +=item.size
}
console.log('缓存总大小',size)
}
})},

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值