小程序-下载文件的方式

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

  downloadFile(originURL, fileName, success)  {
    
    let filePath = `${wx.env.USER_DATA_PATH}/${fileName}`;
      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,
          });
        });
      },
    )
  },

 

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