如何快速清空【本地用户文件】
由于用户本地文件共计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);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码]})[代码]