Henry Aspden
Henry Aspden

Reputation: 382

Creating a .txt file in phonegap 2.0.0

I am trying to add a function to an existing PhoneGap Build (Via Cordova 2.0.0) application.

The app has many existing functions, and this should have been a simple mod.

All I want to be able to do is write a simple HTML page with Cordova 2.0.0 FileWriter API (I think) to save notes as a .txt file (or it could be a .html file) Anything infact that I can then later upload to a PC and copy the text content into word documents/emails etc...

I have begun to use the Apache Cordova API at http://docs.phonegap.com/en/2.0.0/cordova_file_file.md.html#FileWriter however I am having a lot of difficulty understanding this.#

I have also looked at Can't get Phonegaps FileWriter to work however this doesnt look to have even been resolved...

Effectively this is the HTML file I am trying to use...

<form action="" method="post">
    <br>
    <input style="width:100%" class="center" type="text" name="filename" placeholder="Input File Name Here"><br>
    <textarea name="notes" class="center" style="width:100%" placeholder="Please Type Notes Here"></textarea><br>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><input style="width:100%" class="button-big" type="submit" name="save2" value="save"></td>
        <td><input style="width:100%" class="button-big" type="reset" name="clear2" value="clear"></td>
      </tr>
      </table>
    <br>
    </form>

Its a simple HTML form, but I want some JS or something to make the save button save the file to the root with the filename+a predefined file extension (.txt or .htm or .html)

If anybody can help with this, it would be great

thanks,

Henry

Upvotes: 0

Views: 332

Answers (1)

SHANK
SHANK

Reputation: 2968

// 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 );

    var file_name = document.getElementById('filename').value;

    pFileSystemObj.root.getFile( file_name, {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);
}

hope that helps.

Upvotes: 1

Related Questions