微信小程序—在线预览二进制流文件

// 请求 下载表格 接口
  exportExcel (e) {
    let that = this
    let id = that.getPageOptions().id
    let fileName = `【${that.data.holderInfo.nameC}】 ${that.data.bookingInfo.area}-${that.data.productInfo.company}-${that.data.productInfo.plan}`
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    Util.httpRequest('Get', `${Config.exportExcel}/${id}/excel`, {}, 'arraybuffer').then(res => {
      console.log(res)
      wx.hideLoading()
      // that.createCatalog(res.data, fileName) // 创建存放目录
    }).catch(err => {
      console.log(err)
    })
  },
  // 创建存放目录
  createCatalog (arraybuffer, fileName) {
    const rootPath = wx.env.USER_DATA_PATH
    const cachePath = rootPath + '/openFileCache'
    const manage = wx.getFileSystemManager()

    let newFileName = fileName + '.xlsx'
    
    // 判断是否已存在自定义目录
    manage.access({
      path: cachePath,
      success: res => {
        console.log(res)
        let readdir = manage.readdirSync(cachePath)
        console.log(readdir)
        for (let path of readdir) {
          manage.unlinkSync(cachePath + '/' + path)
        }
        this.openFileEv(cachePath, newFileName, arraybuffer)
      },
      fail: err => {
        console.log(err)
        // 新建自定义目录
        manage.mkdir({
          dirPath: cachePath,
          recursive: true,
          success: res => {
            console.log('创建成功')
            this.openFileEv(cachePath, newFileName)
          },
          fail: err => {
            console.log('创建失败')
          }
        })
      }
    })
  },
  // 打开文件
  openFileEv (cachePath, newFileName, arraybuffer) {
    const manage = wx.getFileSystemManager()
     // 显示加载
     wx.showLoading({
      title: '加载中...',
      mask: true
    })
    manage.writeFile({ // 写文件
      filePath: cachePath + '/' + newFileName, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
      data: arraybuffer,
      encoding: "binary", //二进制流文件必须是 binary
      success: res => {
        // 打开文档
        wx.openDocument({
          filePath: cachePath + '/' + newFileName,
          showMenu: true,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail: function () {
            console.log('打开失败')
          }
        })
      }
    })
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值