How to create a separate directory for cordova application and store data in it?

I want to create a specific directory for my cordova project. I have no idea what to do with this. I refer to this, How to transfer a file to the cordova application directory

cordova, an Android application, how to create a subfolder and other links. But it is unclear whether they use any cordova plugins for this, or we can do with pure javascript. They do not work for me. Please suggest if any plugins or features are available.

Thanks.

+5
source share
1 answer

You need to use this plugin: https://www.npmjs.com/package/cordova-plugin-file

I do not know which version of the cordova you are using, and if this plugin has been updated, but when I use it, it does not work with WindowsPhone. Good for Android and iOS.

To get the file system:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, downloadFile, fileSystemFail); 

Download File Function:

 downloadFile: function(fileSystem){ // your code } 

To create a directory:

 var directoryEntry = fileSystem.root; var folderName = "Folder"; directoryEntry.getDirectory(folderName, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail); 

Where onDirectorySuccess and onDirectoryFail are functions such as:

 onDirectorySuccess: function(parent){ console.log(parent); }, onDirectoryFail: function(error){ console.log("Unable to create new directory: " + error.code); } 

To get the path to a file in a directory:

 var filePath = directoryEntry.toURL() + "/" + folderName + "/" + fileName; 

And get the file:

 directoryEntry.getFile(folderName + "/" + fileName, { create: true, exclusive: false }, onFileSuccess, onFileFail); 

To delete a file first, you need to get it:

 directoryEntry.getFile(folderName + "/" + fileName, { create: true, exclusive: false }, onFileSuccessRemove, onFileFail); 

Then in the function of success:

 function onFileSuccessRemove(entry) { entry.remove(); } 

My application was downloading a file, so this is the code to save the file in a directory:

 var fileTransfer = new FileTransfer(); fileTransfer.download(URL, filePath, downloadComplete, downloadFail, true); 

This is not your business, but I hope this helps.

+2
source

Source: https://habr.com/ru/post/1241693/


All Articles