小程序 wx.downloadFile(下载文件 / 打开文件)

2021-11-23  本文已影响0人  web30
重点:一定要在后台提前设置好downloadFile 服务器域名(也就是你要访问的文件地地址),不然无法下载,我当时就遇到这个问题了,还找了半天原因。
image.png
  onHelpCenter(){
    wx.showLoading({
      title: '加载中',
    })
    const fileExtName = 'pdf';
    const randfile = new Date().getTime() + fileExtName;
    const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
    wx.downloadFile({
      url: 'xxx', // 本地服务器上pdf文件的地址
      filePath: newPath,
      success (res) {
        // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          console.log(res)
          let data = res.filePath
          wx.openDocument({
            filePath: newPath,
            showMenu: true,
            fileType: 'pdf',
            success:function(res){
              wx.hideLoading()
            }
          })
        }
      },
      fail:function(res){
        console.log(res)
      }
    })
  }
上一篇 下一篇

猜你喜欢

热点阅读