微信文件下载

// 文件下载,方案一:直接下载文件

export const downLoadFile = (link: string) => {

  wx.downloadFile({

    url: link,

    success(res) {

      console.log('file:', res)

      if (res.statusCode === 200) {

        const filePath = res.tempFilePath

        wx.openDocument({

          filePath: filePath,

          showMenu: true, //显示分享按钮

          success: function () {

            wx.hideLoading()

          }

        })

      }

    }

  })

}

// 导出文件方案二:

export const downLoadBuffer = (buffer: any) => {

  const fs = wx.getFileSystemManager()

  let filePath = `${wx.env.USER_DATA_PATH}/hello.doc`

  fs.writeFile({

    filePath: filePath,

    data: buffer,

    encoding: 'base64',

    success(res) {

      wx.openDocument({

        filePath: filePath,

        showMenu: true, //显示分享按钮

        success: function () {

          wx.hideLoading()

        }

      })

      console.log('res:', res)

    },

    fail(res) {

      console.error("errorxxx:", res)

    }

  })

}

// 导出文件方案三

export const downLoad = () => {

  wx.request({

    url: 'http://10.30.34.159:9204/order/export/word',

    method: 'GET',

    header: {

      'content-type': 'application/octet-stream', // 默认值

    },

    responseType: 'arraybuffer',

    success(res) {

      console.log('res:', res)

      let buffer = res.data

      const fs: any = wx.getFileSystemManager()

      let filePath = `${wx.env.USER_DATA_PATH}/hello.doc`

      fs.writeFile({

        filePath: filePath,

        data: buffer,

        encoding: 'base64',

        success(res: any) {

          wx.openDocument({

            filePath: filePath,

            showMenu: true, //显示分享按钮

            success: function () {

              wx.hideLoading()

            }

          })

          console.log('res:', res)

        },

        fail(res: any) {

          console.error("errorxxx:", res)

        }

      })

    },

    fail(err) {

      console.log('err:', err)

    }

  })

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值