【问题标题】:file path is not found or fail to open/read file Phonegap找不到文件路径或无法打开/读取文件 Phonegap
【发布时间】:2012-10-12 01:59:48
【问题描述】:

我们的代码总是进入失败部分。但是,我们已经改变了几次路径,比如

'file:///android_asset/www/readme.txt' ,
'../android_asset/www/readme.txt', 
'/www/readme.txt", "readme.txt'.
[We have taken "readme.txt"  file in www folder]

我们从下面的链接中获取了代码。

http://docs.phonegap.com/phonegap_file_file.md.html

【问题讨论】:

    标签: iphone android cordova


    【解决方案1】:

    我问了similar question 并没有真正找到解决方案。这是我们方法调用的完整示例:

    window.resolveLocalFileSystemURI("file:///android_asset",
      function(entry){
        console.log(entry.fullPath);},
      function(evt){
        console.log(evt.code);}
    );
    

    但是,在第一阶段,我们只收到了一个未定义的错误代码,而在一个新的测试项目中,我们收到了错误代码 1(找不到文件,line 56)。

    顺便说一句:你意识到你错过了一个反斜杠吗?尝试引用file:///android_asset,如果可行,请告诉我你做了什么:)

    【讨论】:

      【解决方案2】:
       <!DOCTYPE html>
       <html>
       <head>
        <title>FileReader Example</title>
      
      <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>
      <script type="text/javascript" charset="utf-8">
      
      // Wait for PhoneGap to load
      //
      function onLoad() {
          document.addEventListener("deviceready", onDeviceReady, false);
      }
      
      // PhoneGap is ready
      //
      function onDeviceReady() {
          window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
      }
      
      function gotFS(fileSystem) {
          fileSystem.root.getFile("file:///sdcard/example.txt", {create: true}, 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);
      }
      
      </script>
      

      【讨论】:

        猜你喜欢
        • 2019-05-23
        • 2022-12-04
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 2021-02-04
        • 2022-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多