2014-02-19 44 views
2

我看到了多個解決方案如何訪問www文件夾中的文件,但沒有解決方案適用於我。我使用iOS模擬器在iOS下測試應用程序。
我想訪問www文件夾中的文件test.txtPhonegap - 如何訪問www文件夾中的文件?

我目前的解決辦法是這樣的:

var filePathURI = getPhoneGapPath() + "test.txt"; 
window.resolveLocalFileSystemURI(filePathURI, onResolveSuccess, onFail); 

function getPhoneGapPath() { 
    'use strict'; 
    var path = window.location.pathname; 
    var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1); 
    return phoneGapPath; 
}; 

這種解決方案並不爲我工作。我收到errorCode = 2錯誤,這顯然意味着FileError.SECURITY_ERR。不過我嘗試着用resolveLocalFileSystemURI我無法訪問這個文件。

信息:我嘗試以下filePathURI

  1. /Users/UserName/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/GUID/AppName.app/www/test.txt
  2. 文件:///Users/UserName/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/GUID/AppName.app/www/test.txt

誰能給我一個工作的解決方案?

回答

0

我加載像這樣的AJAX我的語言文件...

$.get("test.txt", function(data) { 
    console.log("Load was performed.", data); 
}); 

我認爲您的解決方案,你必須讀訪問添加到您的應用程序 - > config.xml中

<feature name="http://api.phonegap.com/1.0/file" />

+0

與Ajax調用我能夠訪問該文件的內容,但我需要用「resolveLocalFileSystemURI」方法的「FileEnty」對象......我增加了功能,但我仍然得到安全錯誤.. 。它似乎真的沒有權限訪問該文件 –

0

試試這個,我的一部分功能。您首先需要獲取文件系統,然後獲取根路徑。修改它以適應您的需求。

你可以做到以下幾點。

app_FileSystem對我來說是一個全局變量,得到由GetAppFS

分配得到的FS和根路徑後,你可以只是簡單的使用Ajax調用或相應的dataType集的getJSON呼叫。這個對我有用。

同時檢查文檔是有幫助的: http://docs.phonegap.com/en/3.3.0/cordova_file_file.md.html#LocalFileSystem

app_FileSystem.root.fullPath; // Get the app file system root full path 

function GetAppFS() 
{ 
    var self = this; 
    self.state = "";      // store the state of the process for debuggin purposes 
    self.fileSystem = {}; 

    window.requestFileSystem (LocalFileSystem.PERSISTENT, 0, getFileSystemSuccess, dispatchFailure); 

    /** 
    * 
    * Called when we receive a valid file system. Once we do that, we need to ask for all 
    * the documents within the file system. 
    * 
    */ 
    function getFileSystemSuccess (fileSystem) 
    { 
     self.state = "Received File System"; 
     self.fileSystem = fileSystem; 
     app_FileSystem = fileSystem; 
     OnFSReady(); 
    }; 

    /** 
    * 
    * All our functions need a failure callback, so we provide dispatchFailure. If an error occurs, we'll 
    * at least log it to the console, and then call the failure function attached to self.failure(), if any. 
    * 
    */ 
    function dispatchFailure (e) 
    { 
     // some sort of failure :-(
     console.log ("While " + self.state + ", encountered error: " + JSON.stringify(e)); 
     alert ("dev FS ERROR "); 
    }; 
}; 
2

我會建議利用通過的PhoneGap的文件的插件提供的resolveLocalFileSystemURL方法。然後,您可以使用cordova.file.applicationDirectory屬性訪問您的www文件夾所在的位置。

確保你安裝插件:$ cordova plugin add org.apache.cordova.file

然後,你可以使用一個對象,如下面的解析文件,並做必要的操作:

var FileManager = { 

    /** 
    * Execute this.entryHandler against all files and directories in phonegap's www folder 
    */ 
    run: function() { 

    window.resolveLocalFileSystemURL(
     cordova.file.applicationDirectory + 'www/', 
     this.directoryFoundHandler, 
     this.errorHandler 
    ); 

    }, 

    /** 
    * The directory has been successfully read. Now read the entries. 
    * 
    * @param {DirectoryEntry} directoryEntry 
    */ 
    directoryFoundHandler: function (directoryEntry) { 

    var directoryReader = directoryEntry.createReader(); 

    directoryReader.readEntries(
     this.entryHandler, 
     this.errorHandler 
    ); 

    }, 

    /** 
    * Files were successfully found. Parse them! 
    * 
    * @param {Array.<FileEntry>} entries 
    */ 
    entryHandler: function (entries) { 

    entries.forEach(function (entry) { 

     // Deal with your files here 
     if (entry.isDirectory) { 
     // It's a directory might need to loop through again 
     } else { 
     // It's a file, do something 
     } 

    }); 

    }, 


    /** 
    * @param {FileError} error 
    */ 
    errorHandler: function (error) { 

    console.log("ERROR", error); 

    } 

}; 
0

正如我剛纔遇到同樣的問題,但不想使用jQuery,我想我也在這裏發佈我的解決方案。

但在此之前進口的話:在科爾多瓦/電話峽的WWW文件夾中的文件存儲在Android的世界所謂的資產,這意味着:

  • 他們.apk文件分發文件的一部分這是一個壓縮檔案。 Android直接從這個.apk文件中讀取文件,並且不會將這些文件分別存儲在本地文件系統中。
  • 因此這些文件是隻讀的,無法通過Cordova File插件進行訪問。

如果走在corresponding Android sources of Cordova,你可以看到一個深潛,即科爾多瓦過濾所有的URI以「文件」方案,其路徑以「/ android_asset /」和專門使用Android的資產接入功能處理它們。 (從iOS專家那裏聽到科爾多瓦如何在他們的世界中處理它會很有趣)。

這意味着所有使用XMLHttpRequest可能是訪問www文件夾文件的唯一便攜方式,如果您需要訪問該文件內容。 (如果您只需要在文件路徑爲某些系統功能的其它方法可以正常工作。)

下面是代碼,文件名是www文件夾內的路徑沒有「WWW /」前綴:

var readFileInWWWFolder = function(filename, onSuccess, onFailure){ 

    var request = new XMLHttpRequest(); 

    request.onload = function() { 

     var arrayBuffer = request.response; 

     if (arrayBuffer) { 

      onSuccess(new Uint8Array(arrayBuffer)); 
     } 
     else { 

      onFailure(); 
     } 
    }; 

    request.open("GET", filename, true); 
    request.responseType = "arraybuffer"; 
    request.send(); 
}; 

這已使用Cordova 4.3.0和Android 4.4.2(Kitkat)進行測試。

+0

是否會在iPhone應用程序中以相同的方式工作? –

0

一個把戲,工作原理是fs.download從WWW文件夾中的每個文件到科爾多瓦的持久性文件系統。請參閱我的original post

首先,在終端:

  1. npm install cordova-promise-fs
  2. cordova plugin add cordova-plugin-file --save
  3. cordova plugin add cordova-plugin-file-transfer --save

然後,在你的前端:

import CordovaPromiseFS from 'cordova-promise-fs' 

const fs = CordovaPromiseFS({ 
    persistent: true, 
    storageSize: 200 * 1024 * 1024, 
    concurrency: 3 
}) 

如果你的U se React,必須在創建組件Class之前聲明以上代碼,而下面的代碼應該位於組件Class內的自己的函數中。有關更多詳細信息,請參閱我的GitHub comment

window.resolveLocalFileSystemURL(
    cordova.file.applicationDirectory + 'www/epubs/alice.epub', 
    // If successful... 
    (fileSystem) => { 
     const downloadUrl = fileSystem.toURL() 
     const localUrl = 'alice.epub' // the filename it is stored as in the device 
     fs.download(
     downloadUrl, 
     localUrl, 
     (progressEvent) => { 
      if (progressEvent.loaded && progressEvent.total) { 
      console.log('progress', Math.round((progressEvent.loaded/progressEvent.total) * 100)) 
     } 
     } 
    ).then((filedata) => { 
     return fs.toInternalURL(localUrl) 
    }) 
    .then((localPath) => { 
     this.setState({ epubPath: localPath }) 
    }).catch((error) => { 
     console.log('some error happend', error) 
    }) 
    }, 
    // If unsuccessful 
    (err) => { 
    console.log(err) 
    } 
) 
相關問題