File Handling In cordova Using Cordova File Plugin

Posted By : Akhil Dhiman | 17-Feb-2015
File Handling In cordova Using Cordova File Plugin.
Description

In Cordova it is very easy to use file handling all functionality using cordova single plugin. In this plugin we are able to do all the functionality like read, write , delete etc. In this i will show how to use this plugin in cordova.


For this we need to install cordova plugin name "Cordova File Plugin". You can check this plugin from below link.


http://docs.phonegap.com/en/edge/cordova_file_file.md.html


To add this plugin In your project follow the following Procedure.

Installation.

By using Terminal go inside your project directory and enter the following command.

 $ cordova plugin add org.apache.cordova.file
 
To access file storage Of your device

To access file storage of your device you have to use the following method as given below.

        var accessFileStorageToWriteData =function() {
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,createDirectoryFS,failureInGettingFile);
};

In this we are using "window.requestFileSystem" method to access local file system of your device and some call back functions for creating directory and on failure call back function.

Method to check and create Directory in your File System
 var createDirectoryFS = function(fileSystem) {
   fileSystem.root.getDirectory("Folder_Name", {create: true}, localStorageGetFS);
};
 
In this we are using "fileSystem.root.getDirectory" method to check weather directory is existing or not if directory is not existing then this method will create directory with specified name mentioned in method. In this we are using call back function to access file from the file system.
Create file in directory

For Creating file in your specified directory in your local file system storage of your device use following method as given below:

In this we are using "window.requestFileSystem" method to access local file system of your device and some call back functions for creating directory and on failure call back function.

Method to check and create directory in your file system
 var localStorageGetFS = function(dirEntry) {
	dirEntry.getFile("File_name", {
		create : true,
		exclusive : false
	}, gotFileEntry, failureInGettingFile);
};
 

In this we are using "dirEntry.getFile" method to check weather the file is existing or not if file is not existing then it will create your file as per your specified name mentioned in the above method. In this we are also using some call back functions like "gotFileEntry" it is used to get entry in file for performing read, write and some other functionalities and the another one is on failure call back.

File Entry function for performing Read & Write functionalities
 var gotFileEntry = function(fileEntry) {
Ê Ê if(check_rw == "w"){
		fileEntry.createWriter( gotFileWriter, failureInGettingFile);
	}
	else if(check_rw == "r"){
		fileEntry.file( gotFile, failureInGettingFile);
	}
};
 

In this we are using a global variable for performing read or write functionalities according to our requirement.in this "check_rw" global variable tells us that weather to perform read or write functionality. If "check_rw" is equals to "w" then it will call "createWriter" method and if "check_rw" is equals to "r" then it will call read method to perform read functionality.

To write something on file
 var gotFileWriter = function(writer) {
	var dataToStore = "hello everyone Enter your Text here";
	writer.write(dataToStore);
};
 

To write some thing on your file use the above method.

Specifying type to which you want to read as your file
 var gotFile = function(file){
    var readAsText(file);
};
};
 

In this we are performing read as text functionality for reading our file.

Read File As Text
 var readAsText = function(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
       alert("your file reading is completed")
    };
    reader.readAsText(file);
};
 

In this method we are reading the whole file up to at the end of the file.

On failure
 var failureInGettingFile = function(error) {
	console.log(error.code);
};
 

It will call when ever any error occur.

About Author

Author Image
Akhil Dhiman

Akhil is an iPhone and Android application developer with experience in PhoneGap and Swift(Native iOS). Akhil has good experience working with JavaScript, jQuery and Underscore as well.

Request for Proposal

Name is required

Comment is required

Sending message..