I’m currently building an app for my company and a big piece of functionality is downloading courses and storing for offline use. I decided to use Capacitor Filesystem to store the file and then SQLlite to store the file location.
On android and ios the download portion works great and here is the code:
downloadAndStoreFile: async(url, fileName) => {
try{
const dir = "files";
const dirExists = await Filesystem.stat({
path: dir,
directory: Directory.Data,
}).then(stat => stat.type === 'directory')
.catch(() => false);
if (!dirExists) {
await Filesystem.mkdir({
path: dir,
directory: Directory.Data,
recursive: false // directories should not have nested contents
});
}
let path = `${dir}/${fileName}`;
const response = await Http.downloadFile({
url: url,
filePath: path,
fileDirectory: Directory.Data,
});
return response.path;
} catch(error) {
console.log("Error in downloadAndStoreFile of api.service: ", error);
console.log("URL TO HIT: ", url);
}
},
}
Now when I delete the specified file I use the following method:
async deleteFile(path, directory = Directory.Data) {
await Filesystem.deleteFile({
path,
directory,
}).catch((error) => {
console.error("Deletion error: ", error)
});
// Attempt to read the file after deletion
Filesystem.readFile({
path,
directory
}).then(() => {
console.log("File still exists after deletion")
}).catch(() => {
console.log("File successfully deleted")
});
},
I added the second part just recently because when i deleted the file I could not redownload the course. Well adding that I get the File Successfully deleted
console log.
Yet its still on the device.
I know its on the device because I commented out the code where it removes the DB entry and when you remove the DB entry and go to the downloads page it is in fact still there and the video files play perfectly.
Any help would be appreciated.