美文网首页
小程序 wx.downloadFile(下载文件 / 打开文件)

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

作者: web30 | 来源:发表于2021-11-23 16:07 被阅读0次
重点:一定要在后台提前设置好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)
      }
    })
  }

相关文章

网友评论

      本文标题:小程序 wx.downloadFile(下载文件 / 打开文件)

      本文链接:https://www.haomeiwen.com/subject/hpmwtrtx.html