html52.com
我爱小程序-开发者交流社区

如何快速清空【本地用户文件】

问题模块 框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
API和组件 小程序 需求 wx.removeSavedFile 客户端 7.0.5 2.8.0

由于用户本地文件共计10m,

一个保存音频的功能,要求在使用前确保用户本地储存空间剩余10m。所以要进行清空。

是否有一个方便方法。



<button type="primary" bindtap="clearUserData">清空用户文件</button>


Page({
  onReady: function (e) {
    // 创建一个FS对象
    this.fsm = wx.getFileSystemManager();
 
  },
 
  clearUserData: function () {
    const page = this;
    this.fsm.readdir({
      dirPath: `${wx.env.USER_DATA_PATH}/`,
      success: function (res) {
        console.log('readdir', res);
        // 批量删除
        res.files.forEach(element => {
          if (typeof element === 'string') {
            page.fsm.rmdir({
              dirPath: `${wx.env.USER_DATA_PATH}/${element}`,
              recursive: true,
              success: function () {
                console.log('删除文件夹成功', element);
              },
 
              fail: function (e) {
                console.log('删除文件夹失败', e, element);
                wx.removeSavedFile({
                  filePath: `${wx.env.USER_DATA_PATH}/${element}`,
                  success: function (res) {
                    console.log('删除文件成功');
                    console.log(res);
                  }
                });
              }
            })
          }
        });
      }
    })
  }
})


网友回复

ʚ青哥ɞ:

ʚ青哥ɞ:

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付