簡體   English   中英

如何在Phonegap上讀寫文件?

[英]How do I read and write a file on Phonegap?

我正在查看Cordova的FileWriter和FileReader API,並且了解它們是異步的。

通過遵循此處完整示例,我還設法使FileWriter和FileReader分別工作

但是我想知道是否存在一種在寫入文件后立即讀取文件的方法。 下面的代碼顯示了我想在gotFileWritergotFileWriter

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) {
        // Read the file after writing
    };
    writer.write("some sample text");
}

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

文檔完整示例中的FileReader需要一個file對象來讀取某些內容( gotFileWriter方法缺少對其的引用)。 但是,大多數讀取文件的異步過程類似於寫入文件。

如果要在寫入文件后讀取文件,是否必須通過調用window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);再次啟動整個異步過程window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); onwriteend函數中? 以及gotFileEntry一種調用fileEntry.file() gotFileEntry方法? 還是有一種方法可以直接從gotFileWriter方法中獲取file對象,而不必重復這些步驟?

有誰知道最快的方法嗎?

創建,讀取,編輯和刪除示例

在顯示示例之前,我想說的是在示例中使用externalDataDirectory (file:///data/user/0/com.adobe.phonegap.app/files/),因為此目錄是公共目錄,因此您可以檢查手機上存在和更改的文件。 生成的文件應該在您的內部存儲器中。

這些示例旨在相互補充。

生成文件>讀取生成的文件>編輯生成的文件>刪除生成的文件。

一個如何寫文件的例子:

 //
 //resolve url for directory entry for putting in new file
 //cordova.file.externalDataDirectory / file:///data/user/0/com.adobe.phonegap.app/files/
 //
 window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function success(dataDirEntry) {
     //create new file         
     dataDirEntry.getFile("test.txt", { create: true, exclusive: false }, function (newFileEntry) {

         // Create a FileWriter object for our newFileEntry             
         newFileEntry.createWriter(function (fileWriter) {

             fileWriter.onwriteend = function () {
                 console.log("Successful file write...");                                   
             };

             fileWriter.onerror = function (e) {
                 console.log("Failed file write: " + JSON.stringify(e));
             };

             //type can be 'text/plain' or newFileEntry.type for .txt             
             var blob = new Blob(['test it works'], { type: newFileEntry.type });
             //console.log(blob);                               

             fileWriter.write(blob);                               
         });
    }, function(e) { console.log('creating file error'); console.log(e); });
}, function error(e) { console.log('resolving directory error'); console.log(e);  });

有關如何讀取文件的示例:

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory+'test.txt', function success(fileEntry) {
    //read file
    fileEntry.file(function (file) {
        var reader = new FileReader();
        reader.onloadend = function() {
            var fileData = this.result;
            console.log(fileData);
        };
        reader.readAsText(file);
    }, function(e) { console.log('opening file error'); console.log(e); });
}, function error(e) { console.log('resolving directory error'); console.log(e);  });

有關如何編輯文件的示例:

該示例中使用了javascript函數replace 我還沒有找到另一種編輯文件的方法。

 //resolve url for file entry for reading the file    
 //
 window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory+'test.txt', function success(fileEntry) {
     //read file
     fileEntry.file(function (file) {
         var reader = new FileReader();
         reader.onloadend = function() {
             var fileData = this.result;
             console.log(fileData);
             //replace test with yes
             fileData = fileData.replace('test', 'yes');

             // write the edited filedata to the file
             // Create a FileWriter object for our fileEntry
             fileEntry.createWriter(function (fileWriter) {

                 fileWriter.onwriteend = function () {
                     console.log("Successful file write...");
                 };

                 fileWriter.onerror = function (e) {
                     console.log("Failed file write: " + JSON.stringify(e));
                 };

                 //type can be 'text/plain' or newFileEntry.type for .txt
                 var blob = new Blob([fileData], { type: fileEntry.type });
                 //console.log(blob);

                 fileWriter.write(blob);
             });
         };
         reader.readAsText(file);
     }, function(e) { console.log('opening file error'); console.log(e); });
}, function error(e) { console.log('resolving directory error'); console.log(e);  });

有關如何刪除文件的示例:

window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory+'test.txt', function success(fileEntry) {
    fileEntry.remove(function() {
        console.log('file deleted');
    }, function(e) {
        console.log('file not deleted');
        console.log(e); 
    });
});

在啟動應用程序時使用此功能。 它將讀取和寫入文件。 試試吧。

 function onDeviceReady() {
               window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
               window.resolveLocalFileSystemURI("file:///example.txt", onResolveSuccess, fail);
               var isApp = 'yes';
               var root = this;
               cb = window.plugins.childBrowser;
               call();
           }

           function onFileSystemSuccess(fileSystem) {
               console.log(fileSystem.name);
           }

           function onResolveSuccess(fileEntry) {
               console.log(fileEntry.name);
           }

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


           function call(){
               window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successDirectoryReader, null);

           }
function successDirectoryReader(fileSystem)
           {
               try {
                   var dirEntry = fileSystem.root;
                   var directoryReader = dirEntry.createReader();
                   directoryReader.readEntries(success,failure);
               } catch (e) {
                   alert(e);
               }
           }

如何讀取文本文件的內容並將其寫入<div>使用普通的 JavaScript?</div><div id="text_translate"><p> <strong>問題:</strong>如何從文本文件中讀取內容(與 html 頁面一起保存在服務器上)並將該內容寫入 HTML 頁面上的<em>“innerHTML”</em>中?</p><p> <strong>上下文:</strong>我想創建一個用作博客的 static 網站。 但是,我希望只有一個帖子頁面,每個帖子都從文本文件中讀取並寫入頁面的主 div。 文本文件中的內容將用所需的 html 標記進行標記,以便它們在寫入 div 時像 html 一樣 function (但文件本身將保存為<em>.txt</em>文件)</p><p> 理想情況下,文本文件應該能夠存儲在與調用它的 html 頁面(和/或腳本文件)不同的文件夾中。</p><p> <strong>附加信息:</strong>我遇到的大多數解決方案都與讀取用戶上傳的文件有關。 這不是我想要理解的。 我試圖了解如何從作為網站資產一部分存儲的文本文件中讀取數據。</p></div>

[英]How do I read a text file's content and write it to a <div> using plain JavaScript?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 phonegap讀寫json文件 當我具有要讀取的文件路徑時,如何在cordova(離子)文件中讀寫文件? 如何使用phonegap讀取低功耗藍牙串口? 我正在創建一個Mac應用程序,該應用程序需要讀取和寫入HTML文件。 我該如何實現? 如何在HTML5 Javascript Canvas Phonegap游戲中從文本文件讀取和寫入? 如何讀取文本文件的內容並將其寫入<div>使用普通的 JavaScript?</div><div id="text_translate"><p> <strong>問題:</strong>如何從文本文件中讀取內容(與 html 頁面一起保存在服務器上)並將該內容寫入 HTML 頁面上的<em>“innerHTML”</em>中?</p><p> <strong>上下文:</strong>我想創建一個用作博客的 static 網站。 但是,我希望只有一個帖子頁面,每個帖子都從文本文件中讀取並寫入頁面的主 div。 文本文件中的內容將用所需的 html 標記進行標記,以便它們在寫入 div 時像 html 一樣 function (但文件本身將保存為<em>.txt</em>文件)</p><p> 理想情況下,文本文件應該能夠存儲在與調用它的 html 頁面(和/或腳本文件)不同的文件夾中。</p><p> <strong>附加信息:</strong>我遇到的大多數解決方案都與讀取用戶上傳的文件有關。 這不是我想要理解的。 我試圖了解如何從作為網站資產一部分存儲的文本文件中讀取數據。</p></div> PhoneGap構建-如何將文件保存到移動設備? 如何在phonegap上的android中創建或寫入文件? 如何在js中寫入本地文件(phonegap) 如何使用PhoneGap Javascript在android中讀取文件
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM