2012-05-05 57 views
5

我正在嘗試使用Phonegap [cordova 1.7.0]在iOS上使用文件。 我讀了如何訪問文件並在電話差距的API Documentation上讀取它們。但是我不知道,當文件被讀取時它會被寫入哪裏? &如何輸出文本,圖像或iPhone屏幕上包含的任何文本?使用Phonegap訪問文件

下面是我使用的代碼:

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    fileEntry.file(gotFile, fail); 
} 

function gotFile(file){ 
    readDataUrl(file); 
    readAsText(file); 
} 

function readDataUrl(file) { 
    var reader = new FileReader(); 
    reader.onloadend = function(evt) { 
     console.log("Read as data URL"); 
     console.log(evt.target.result); 
    }; 
    reader.readAsDataURL(file); 
} 

function readAsText(file) { 
    var reader = new FileReader(); 
    reader.onloadend = function(evt) { 
     console.log("Read as text"); 
     console.log(evt.target.result); 
    }; 
    reader.readAsText(file); 
} 

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

回答

4

這對我的情況下,任何人什麼工作需要它:

function ReadFile() { 
    var onSuccess = function (fileEntry) { 
    var reader = new FileReader(); 
    reader.onloadend = function (evt) { 
     console.log("read success"); 
     console.log(evt.target.result); 
     document.getElementById('file_status').innerHTML = evt.target.result; 
    }; 
    reader.onerror = function (evt) { 
     console.log("read error"); 
     console.log(evt.target.result); 
     document.getElementById('file_status').innerHTML = "read error: " + evt.target.error; 
    }; 

    reader.readAsText(fileEntry); // Use reader.readAsURL to read it as a link not text. 
    }; 

    console.log("Start getting entry"); 
    getEntry(true, onSuccess, { create: false }); 
}; 
+1

我在哪裏指定我想讀的文件? – donkey

+0

+1我在哪裏指定文件? –

+0

@john_cat&oasisweng:我已經添加了一個評論,並提供了一個我認爲可以解決您的問題的示例。 HTH! – sherb

3

如果您正在使用的PhoneGap(科爾多瓦)1.7.0,以下頁面將在iOS的工作,index.html中替換以下模板

<!DOCTYPE html> 
<html> 
    <head> 
    <title>FileWriter Example</title> 
    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

    // Wait for Cordova to load 
    // 
    document.addEventListener("deviceready", onDeviceReady, false); 

    // Cordova is ready 
    // 
    function onDeviceReady() { 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    } 

    function gotFS(fileSystem) { 
     fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail); 
    } 

    function gotFileEntry(fileEntry) { 
     fileEntry.createWriter(gotFileWriter, fail); 
    } 

    function gotFileWriter(writer) { 
     writer.onwriteend = function(evt) { 
      console.log("contents of file now 'some sample text'"); 
      writer.truncate(11); 
      writer.onwriteend = function(evt) { 
       console.log("contents of file now 'some sample'"); 
       writer.seek(4); 
       writer.write(" different text"); 
       writer.onwriteend = function(evt){ 
        console.log("contents of file now 'some different text'"); 
       } 
      }; 
     }; 
     writer.write("some sample text"); 
    } 
    function fail(error) { 
     console.log(error.code); 
    } 
    </script> 
    </head> 
    <body> 
    <h1>Example</h1> 
    <p>Write File</p> 
    </body> 
</html>
+1

非常感謝=)可是我問了讀功能=) –

+1

@sana酷..... –

+0

但真的謝謝你=)) –

5

由於科爾多瓦3.5(至少),的10個對象只接受File對象,而不是FileEntry對象(我不確定以前的版本)。

下面是一個例子,它會將本地文件readme.txt的內容輸出到控制檯。與Sana的例子不同的是撥打FileEntry.file(...)。這將提供撥打FileReader.readAs功能所需的File對象。

function readFile() { 
    window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fileSystem) { 
     fileSystem.root.getFile('readme.txt', 
      {create: false, exclusive: false}, function(fileEntry) { 
       fileEntry.file(function(file) { 
        var reader = new window.FileReader(); 
        reader.onloadend = function(evt) {console.log(evt.target.result);}; 
        reader.onerror = function(evt) {console.log(evt.target.result);}; 
        reader.readAsText(file); 
       }, function(e){console.log(e);}); 
      }, function(e){console.log(e);}); 
    }, function(e) {console.log(e);}); 
} 
相關問題