2012-05-05 51 views
5

Tôi đang cố gắng làm việc với các tệp trên iOS, sử dụng Phonegap [cordova 1.7.0]. Tôi đọc cách truy cập các tập tin và đọc chúng trên API Documentation khoảng cách điện thoại. Nhưng tôi không biết, khi tập tin được đọc, nơi nó sẽ được viết? & Làm cách nào để xuất văn bản, hình ảnh hoặc bất kỳ văn bản nào có chứa trên màn hình iPhone?Truy cập các tập tin bằng cách sử dụng Phonegap

Dưới đây là đoạn code tôi đang sử dụng:

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

Trả lời

4

đó là những gì làm việc cho tôi trong trường hợp bất cứ ai cần nó:

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

ở đâu để tôi xác định tập tin tôi muốn đọc? – donkey

+0

+1 Tôi chỉ định tệp ở đâu? –

+0

@john_cat & oasisweng: Tôi đã thêm nhận xét với ví dụ mà tôi nghĩ là giải quyết câu hỏi của bạn. HTH! – sherb

3

Nếu bạn đang sử dụng PhoneGap (Cordova) 1.7.0, sau trang sẽ làm việc trong iOS, hãy thay bản mẫu sau trong 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

Thanks a lot =) Nhưng tôi đã yêu cầu các chức năng đọc =) –

+1

@sana mát ..... –

+0

Nhưng thực sự cảm ơn bạn =)) –

5

Tính đến Cordova 3,5 (ít nhất), FileReader đối tượng chỉ chấp nhận đối tượng File, không phải đối tượng FileEntry (Tôi không chắc chắn về các bản phát hành trước đó).

Dưới đây là ví dụ sẽ xuất nội dung tệp cục bộ readme.txt vào bảng điều khiển. Sự khác biệt ở đây từ ví dụ của Sana là gọi đến FileEntry.file(...). Điều này sẽ cung cấp đối tượng File cần thiết cho cuộc gọi đến các chức năng FileReader.readAs.

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);}); 
} 
Các vấn đề liên quan