2012-05-05 15 views
5

का उपयोग कर का उपयोग करके फ़ाइलें, Phonegap का उपयोग कर [कॉर्डोबा 1.7.0]। मैं कैसे फ़ाइलों तक पहुँचने और फोन अंतराल के API Documentation पर उन्हें पढ़ने के लिए पढ़ें। लेकिन मुझे नहीं पता, जब फ़ाइल पढ़ी जाती है तो यह कहां लिखा जाएगा? & मैं आईफोन स्क्रीन पर पाठ, छवि, या जो कुछ भी टेक्स्ट रख रहा हूं उसे आउटपुट कैसे कर सकता हूं?पहुँच मैं आईओएस पर फाइलों के साथ काम करने के लिए कोशिश कर रहा हूँ 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 का उपयोग कर रहे हैं, तो निम्न पेज आईओएस में काम करेंगे, तो निम्न टेम्पलेट 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

Cordova 3.5 (कम से कम),के रूप में 10 वस्तुओं केवल File वस्तुओं, नहीं FileEntry वस्तुओं (मैं पहले रिलीज के बारे में यकीन नहीं है) स्वीकार करते हैं।

यहाँ एक उदाहरण है कि सामग्री स्थानीय फाइल readme.txt कंसोल के लिए उत्पादन होगा। साना के उदाहरण से यहां अंतर FileEntry.file(...) पर कॉल है। यह File वस्तु 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);}); 
} 
संबंधित मुद्दे

 संबंधित मुद्दे