2013-01-18 45 views
0

我正在嘗試向現有的PhoneGap Build(Via Cordova 2.0.0)應用程序添加一個函數。在phonegap 2.0.0中創建.txt文件

該應用程序有很多現有的功能,這應該是一個簡單的國防部。

我想要做的就是用Cordova 2.0.0 FileWriter API編寫一個簡單的HTML頁面(我認爲)將筆記保存爲.txt文件(或者它可以是.html文件)任何事實上然後我可以在以後上傳到PC和複製的文本內容轉換成word文檔/電子郵件等...

我已經開始使用Apache科爾多瓦API在http://docs.phonegap.com/en/2.0.0/cordova_file_file.md.html#FileWriter但我遇到了很多困難,理解這個的。#

我也看過Can't get Phonegaps FileWriter to work然而這看起來甚至沒有解決...

實際上,這是HTML文件,我試圖用...

<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> 

它是一個簡單的HTML表單,但我想一些JS或東西,使保存按鈕將文件保存到與文件名根+預定義的文件擴展名(.txt或.htm或.html)

如果有人能在這方面幫助,這將是偉大

感謝,

亨利

回答

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

希望有所幫助。

+0

這將使用表單字段來創建文件名? 我需要從

+0

我已經編輯了我的答案,現在它會選取您的輸入控件的值。只需向您的控件提供'id'屬性即可。這應該對你有所幫助。 – SHANK