Piyush
Piyush

Reputation: 3071

How to create file in Sdcard using phonegap

How to create file and save file in Android Sdcard Using phonegap ?

Upvotes: 5

Views: 3186

Answers (1)

SHANK
SHANK

Reputation: 2958

// create a file writer object
function CreateFileWriter()
{
    // request the file system object
window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, OnFileSystemSuccess,fail);
}

function OnFileSystemSuccess( pFileSystemObj )
{
    console.log( pFileSystemObj.name );
    console.log( pFileSystemObj.root.name );

    pFileSystemObj.root.getFile( "file_name.txt", {create: true, exclusive: false}, OnFileGetSuccess, fail);
}

function OnFileGetSuccess( pFileEntryObj )
{
pFileEntryObj.createWriter( function(pWriterObj){ 
    gWriterObj  = pWriterObj; 
    }, fail );
}

function fail(evt)
{
    console.log(evt.target.error.code);
}

Here create file writer method provides a handle to the file system. In the success function, we get the file called, 'file_name.txt', if exists it opens up otherwise creates it.

Upvotes: 7

Related Questions