微信小程序文件保存wx.env.USER_DATA_PATH地址报错

微信小程序文件保存地址报错

 let filePath = wx.env.USER_DATA_PATH +"/"+name
    console.log(filePath)
    let fs = wx.getFileSystemManager()
    fs.writeFileSync({
      filePath: "/"+name,
      data: file_bt,
      encoding: 'base64',
      success(res) {
        console.log(res)
        console.log(filePath)
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      },
      fail(res) {
        console.error(res)
      }
    })
  }

一直报保存地址的错误

img

为什么地址是http://,这个问题和在电脑上有关系么,看教程都是用的这个地址参数啊

    let filePath = wx.env.USER_DATA_PATH + "/" + name
    console.log(filePath)
    let fs = wx.getFileSystemManager()
    // 两处问题

    // 第一你使用的是异步的写法,但是用的是同步的API
    // fs.writeFileSync({ 
    fs.writeFile({
      // 第二这里路径传值要使用上面拼接好的地址
      // filePath: "/" + name,
      filePath: filePath,
      data: file_bt,
      encoding: 'base64',
      success(res) {
        console.log(res)
        console.log(filePath)
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      },
      fail(res) {
        console.error(res)
      }
    })