小程序保存用户本地文件问题
发布于 6 年前 作者 yong65 1142 次浏览 来自 问答
  • 调用appendFileSync()函数保存“用户本地文件”时,在Windows端的开发工具中是可以正常保存的。然而在手机上无法保存(调用读取readFileSync()函数读取不到数据),同时查看微信文件保存的文件发现是0b大小。?
  • 开发工具中能够读取保存的数据,在手机端就无法读取到文件中的数据

-结果对比:



图一  开发工具中的效果


-以下是Demo代码:

.js文件

btn_saveFile: function(){
    const FileSystemManager = wx.getFileSystemManager();
    if(this.data.save_str == null){
      //把数组中的数据转换成字符串
      this.data.save_str = this.obj2string(this.data.display_data_arr);
      FileSystemManager.writeFileSync(`${wx.env.USER_DATA_PATH}/VR_location.txt`, this.data.save_str, 'utf8');
    }
    else{
      //把数组中的数据转换成字符串
      this.data.save_str = this.obj2string(this.data.display_data_arr);
      FileSystemManager.appendFileSync(`${wx.env.USER_DATA_PATH}/VR_location.txt`, this.data.save_str, 'utf8');
    }
    //把数组中的数据转换成字符串
    // this.data.save_str = this.obj2string(this.data.display_data_arr);
    // console.log('转换:'+this.obj2string(this.data.display_data_arr));
    
    let tt = FileSystemManager.saveFileSync(
      `${wx.env.USER_DATA_PATH}/VR_location.txt`,
      `${wx.env.USER_DATA_PATH}/VR_location.txt`);
    console.log('保存路径:' + tt);
 
    wx.showToast({
      title: '文件保存成功!',
      icon: 'success',
      duration: 1500
    });
     
    const temp = FileSystemManager.readFileSync(`${wx.env.USER_DATA_PATH}/VR_location.txt`, 'utf8');
    console.log("文件内容:" + temp);
 
    //清除数据
    // for()
  }
3 回复

有走fail回调么?

你这里为什么要调用 saveFileSync ?

其他手机上也试过,都是同样的问题

回到顶部