小程序-下载文件的方式

正常下载的时候,出现下载的时候空白,基本是格式出现问题,或者是文件名出现问题。文件不能是中文

  downloadFile(originURL, fName, success)  {
    const fileName = (val) => {
    const end =
originURL.lastIndexOf('=');

     let name = '';

    if(end > -1) {
      name = originURL.subString(end+1)
      if(val == '协议') return name + '.pdf';
      if(val == '客户') return name + '.xls';
      if(val == '文档') return name + '.doc';
    }
  }

    

  fName = fileName(fName)

 


    let filePath = `${wx.env.USER_DATA_PATH}/${fName}`;
      wx.downloadFile({
        url: originURL,
        filePath: filePath,
        header: {
          'sessionId': getApp().globalData.openId,
        },
        success: (res) => {
          wx.hideLoading();
          if(res.statusCode == 200) {
            if (success) {
              success(res);
            }
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '下载失败',
            icon: 'none',
          });
        },
      });
  },

  onSaveDocument(val, callback) {
    wx.getSystemInfo({
      success: function (res) {
        if (res.platform === 'windows') {
          wx.saveFileToDisk({
            filePath: val,
          })
        } else callback()
      }
    });
  },
  opFile: function (e) {
    const fileName = e.currentTarget.dataset.name;
    const eng = e.currentTarget.dataset.eng;
    const type = e.currentTarget.dataset.type;
    const geturl = 'https://www.baidu.com/doc/' + eng;
    const originURL = decodeURI(geturl);

    const that = this;
    const fileValue = {
      data: ''
    };
    this.downloadFile(originURL, fileName,
      (res) => {
        fileValue.data = res.filePath;
      
        that.onSaveDocument(res.filePath, () => {
          wx.openDocument({
            filePath: res.filePath,
            showMenu: true,
          });
        });
      },
    )
  },

 

另一个 写法

const downloadFile = (mothodName, fileName, success, params) => {
  wx.showLoading({ title: '下载中' });
  let originURL = httpUrl + mothod[mothodName];
  if(params) originURL += params;
  
  let filePath = `${wx.env.USER_DATA_PATH}/${fileName}`;
  getOpenId(mothodName,function () {
    wx.downloadFile({
      url: originURL,
      filePath: filePath,
      header: {
        'sessionId': getStorage('u_token'),
      },
      success: (res) => {
        wx.hideLoading();
        if(res.statusCode == 200) {
          if (success) {
            success(res);
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '下载失败',
          icon: 'none',
        });
      },
    });
  })
};

  exportFiles: function() {
    const that = this;
    const url = 'exportAppAgencySettleInfoPdf';

    const data = this.data;
    const snapshot = data.snapshot;
    const allDatas = data.allDatas;
    const agencyId = snapshot.aid;
    const agencyResultId = snapshot.id;
    const beginDt = snapshot.dt;
    
    const fileName = allDatas.agencyName +'-'+ beginDt + '.pdf';

    const params = `?agencyId=${agencyId}&agencyResultId=${agencyResultId}&beginDt=${beginDt}`

    const fileValue = {
      data: ''
    };
    https.downloadFile(url, fileName,
      (res) => {
        fileValue.data = res.filePath;
        that.setData({
          fileValue,
        });

        that.onSaveDocument(res.filePath, () => {
          wx.openDocument({
            filePath: res.filePath,
            showMenu: true,
          });
        });
      },
      params
    )
  },

  onSaveDocument(pathval, callback) {
    wx.getSystemInfo({
      success: function (res) {
        if (res.platform === 'windows') {
          wx.saveFileToDisk({
            filePath: pathval,
          })
        } else callback()
      }
    });
  },
  

 

posted @ 2025-10-27 14:41  微宇宙  阅读(31)  评论(0)    收藏  举报