主要用到了微信原生小程序的两个api,wx.downloadFile和wx.shareFileMessage

exportQuoteOrder是点击导出时调用的方法。调用导出接口,后端会返回文件的地址url,包含了文件名信息等

exportQuoteOrder() {
  app.requestPost(this, {
    id: this.data.exportId
  }, exportSaleApi, res => {
    if (res.data.code === 200) {
      this.downloadAndShare(res.data.data);
    }
  });
},
downloadAndShare(path) {
    let that = this;
    wx.showLoading({
      title: '下载中...'
    });
    // 下载地址 api是服务器请求地址
    const filePath = `${api}/${path}`;
    // 文件名称
    let fName = path.split("/")[1];
    console.log(filePath, fName, '------文件下载地址')
    wx.downloadFile({
      url: filePath, // 文件本身的url
      success(res) {
        if (res.statusCode === 200) {
          wx.hideLoading();
          // 下载成功,处理文件 分享或打开
          wx.shareFileMessage({
            filePath: res.tempFilePath,
            fileName: fName,
            success: () => {},
          });
        }
      },
      fail(err) {
        console.log('下载失败', err);
        wx.showToast({
          title: '下载失败',
          icon: 'none'
        });
      }
    });
  }

 

下载和分享api还有很多参数,具体的可以在官网查看

DownloadTask | 微信开放文档微信开发者平台文档https://developers.weixin.qq.com/miniprogram/dev/api/network/download/wx.downloadFile.html这世界很喧嚣,做你自己就好

更多推荐