1.const fs = wx.getFileSystemManager();
const fs = wx.getFileSystemManager();
2. FileSystemManager.readdir()获取你用FileSystemManager.writeFile存入的列表
fs.readdir({
dirPath: wx.env.USER_DATA_PATH,
success(res) {
//res.files为存入的列表路径
}
})
3.FileSystemManager.unlink()将对应的路径结果进行删除
fs.unlink({
filePath:'这里是你存储的路径',
success(succ){
console.log('文件删除成功', succ);
}
fail(e) {
console.log('文件删除失败', e)
}
})
注意:filePath一定要和你存储的路径一模一样才可以删除成功!不然会报没有删除权限errMsg:"unlink:fail fail permission denied, open "http://usrchuwanning1.png""(这个小程序的报错提示也是真的坑.)

最后贴上偶的源代码:
const fs = wx.getFileSystemManager();
fs.readdir({
dirPath: wx.env.USER_DATA_PATH,
success(res) {
let indexVal = res.files.length;
res.files.forEach((el) => {
let name = (wx.env.USER_DATA_PATH+el).replace(/usr/g, "usr/");
fs.unlink({
filePath:name,
fail(e) {
console.log('文件删除失败:', e)
that.GoCanvas()
},
success(succ){
indexVal++;
if(indexVal==0){
that.GoCanvas()
}
console.log('文件删除成功:', succ);
}
})
})
}
})
本文介绍了如何在微信小程序中使用FileSystemManager进行文件操作,包括通过wx.getFileSystemManager()获取文件系统管理器,使用readdir读取指定目录下的文件列表,以及使用unlink删除文件。在删除文件时,强调了 filePath 必须与存储路径完全一致才能成功删除,否则会遇到权限错误。最后展示了一段源代码实例,演示了批量删除文件的过程。
2024

被折叠的 条评论
为什么被折叠?



