目前FileSystemManager存在的4个问题
发布于 6 年前 作者 yangyan 498 次浏览 来自 问答

File处理是非常重要的类,稍有问题,就可能导致系统数据错误,目前发现FileSystemManager有4个问题:

1、removeSavedFile(Object)方法已经失效。无法删除saveFile保存文件。2个月前测试还是正常的。导致以前使用此方法的程序全部出错。文档上没有任何说明。

2、unlinkSync()方法代替了removeSaveFile方法。文档依然没有任何说明。方法命名也是有问题的,unlink怎么也无法跟删除文件联系在一次。以后命名用delFile,deleteFile等

3、accessSync()方法用于判断文件是否存在。尽然是通过函数报错实现的。"如果执行方法报错,说明文件不存在。"这个是很原始的思维。方法返回true,false是比较优雅的,代码实现:

        try{

            wx.getFileSystemManager().accessSync(path);

            return true;

        }catch(e){

            return false;

        }

4、getSavedFileList()方法无法获得saveFile保存的文件列表(估计跟removeSaveFile是相同的问题)

按照经验来看,这个类应该有不止以上4个问题,应该还有更多的问题。请抓紧排查修复。

4 回复

同意楼主,希望官方,认证排查完善一下FileSystemManager API相关方法,的确问题有点多。

资源存储文件到手机缓存问题太多,导致部分用户再进不了游戏,迫不得已把缓存到本地去掉了

我这边不能重现,提供一下代码片段?

保存文件之后,测试getSavedFileList()方法,正常,但文件为空,再测试AccessSync()文件存在的,再测试removeSavedFile,提示找不到文件

unlink方法已注释,打开之后可以看到运行正常


以下为日志输出

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

以下为代码

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

export default class Main {
  constructor() {
    this.testFile(this);
  }
  
  testFile(mainobj) {
    var downloadTask = wx.downloadFile({
      url: "https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/dNEBuK6.png",
      success: function (res) {
        if (res.statusCode === 200) {
          var fsm = wx.getFileSystemManager();
          fsm.saveFile(
            {
              tempFilePath: res.tempFilePath,
              filePath: wx.env.USER_DATA_PATH + "/a.png",//保存到特定目录
              success: function (res) {
                console.log("保存文件成功:"+res.savedFilePath);
 
                mainobj.testgetSavedFileList();
                mainobj.testAccessSync(res.savedFilePath);
                mainobj.testremoveSavedFile(res.savedFilePath);
                // mainobj.testunlink(res.savedFilePath);
                // mainobj.testAccessSync(res.savedFilePath);
 
              }
 
            }
          );
        } else {
          console.log("download url error,status:" + res.statusCode);
          console.log("download url:" + url);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
 
  testgetSavedFileList() {
    wx.getFileSystemManager().getSavedFileList(
      {
        "success": function (res) {
          console.log("getSavedFileList() 测试:");
          console.log(res);
        },
        "fail": function (res) {
          console.log(res);
        }
      }
    );
  }
 
  testAccessSync(path) {
    var exist = false;
    try {
      wx.getFileSystemManager().accessSync(path);
      exist = true;
    } catch (e) {
      exist = false;
    }
    console.log("accessSync() 测试是否存在:" + exist);
  }
  
  testremoveSavedFile(path) {
    wx.getFileSystemManager().removeSavedFile(
      {
        "filePath":path,
        "success": function (res) {
          console.log("removeSavedFile() success:");
          console.log(res);
        },
        "fail": function (res) {
          console.log("removeSavedFile() fail:");
          console.log(res);
        }
      }
    );
  }
 
  testunlink(path) {
    wx.getFileSystemManager().unlinkSync(path);
    console.log("unlink删除");
  }
}
回到顶部