• uniapp文件下载


    uni.downloadFile({
      //只能是GET请求
      url: base.baseUrl + `/electricStation/exportData?siteId=1&startTime=${e[0]}&endTime=${e[1]}`, //请求地址
      header: {
        Authorization: 'Bearer ' + uni.getStorageSync('SET_TOKEN') //请求头。
      },
      success: res => {
      //下载成功
      if (res.statusCode === 200) {
        var sFilePath = res.tempFilePath;
        //保存文件
        uni.saveFile({
          tempFilePath: res.tempFilePath, //下载成功之后返回的临时路径
          success: e => {
            console.log(e);
            var savedFilePath = e.savedFilePath;

            //解决下载返回文件名和保存文件名不一致问题
            this.fSetFileName(e.savedFilePath, sFilePath);
          },
          fail: e => {
            uni.showToast({
              title: `保存失败` + e
            });
          }
         });
        }
      },
      fail: e => {
        uni.showToast({
          title: `文件下载失败` + e,
          icon: 'none'
        });
      }
    });

    fSetFileName(sFilePath, sFileName) {
      var sFileName = sFileName.split('/')[sFileName.split('/').length - 1]; //原来的文件名
      var aFileUrl = sFilePath.split('/').splice(0, sFilePath.split('/').length - 1); //saveFile API保存的本地地址
      var url = base.baseUrl + `/electricStation/exportData?siteId=1&startTime=${this.range[0]}&endTime=${this.range[1]}`; //请求地址
      let dtask = plus.downloader.createDownload(
        url,
        {
          filename: 'file://' + aFileUrl.join('/') + '/' + sFileName //利用保存路径,实现文件的重命名
        },
        (d, status) => {
          if (status == 200) {
            let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
            this.fGetSavedFileList();

            uni.openDocument({
              filePath: fileSaveUrl,
              fail: e => {
                uni.showToast({
                  title: `打开失败` + e
                });
              }
            });
          } else {
            //下载失败
            plus.downloader.clear(); //清除下载任务
          }
        }
      );
      dtask.setRequestHeader('Authorization', 'Bearer ' + uni.getStorageSync('SET_TOKEN'));
      dtask.start();
    },

    // 获取已下载列表
    fGetSavedFileList() {
      uni.getSavedFileList({
        success: res => {
          res.fileList.forEach(oData => {
            let aRray = oData.filePath.split('/');
            let sFileName = aRray[aRray.length - 1].split('.')[0];
            if (parseFloat(sFileName).toString() == 'NaN') {
              //这里是过滤掉文件名是时间戳的文件
              console.log(res.fileList);
            } else {
              //这里是把时间戳的文件删掉
              plus.io.resolveLocalFileSystemURL(oData.filePath, entry => {
                //获取文件对象
                entry.remove(
                  entry => {
                    plus.console.log('Remove succeeded');
                   },
                  e => {
                    alert(e.message);
                  }
                );
              });
            }
          });
        }
      });
    }

  • 相关阅读:
    HDNOIP201404最短路径
    BJOI2015 Day3
    BJOI2015 Day2
    BJOI2015 Day1
    BZOJ4012 [HNOI2015]开店
    hdu2159(二维完全背包)
    hdu3496(二维背包)
    hdu3033(变形分组背包)
    hdu1267(递推)
    hdu1503(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/yqq520/p/16054665.html
Copyright © 2020-2023  润新知