最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access files using using Phonegap 3.3.0 for ios - Stack Overflow

programmeradmin2浏览0评论

I'm trying to work with files on IOS, using Phonegap[cordova 3.3.0]. I read how to access files and read them on the API Documentation of phone gap. also added plugin like this

  $ cordova plugin add org.apache.cordova.file
    $ cordova plugin ls
    [ 'org.apache.cordova.file' ]
    $ cordova plugin rm org.apache.cordova.file

 $ cordova plugin add org.apache.cordova.file-transfer
    $ cordova plugin ls
    [ 'org.apache.cordova.file',
      'org.apache.cordova.file-transfer' ]
    $ cordova plugin rm org.apache.cordova.file-transfer

function gotFS(fileSystem) is not calling after onDeviceReady() function.

Here's the code I'm using:

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

This code is working for android. But for Ios, I am getting ReferenceError: Can't find variable: LocalFileSystem in this line -

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

I'm trying to work with files on IOS, using Phonegap[cordova 3.3.0]. I read how to access files and read them on the API Documentation of phone gap. also added plugin like this

  $ cordova plugin add org.apache.cordova.file
    $ cordova plugin ls
    [ 'org.apache.cordova.file' ]
    $ cordova plugin rm org.apache.cordova.file

 $ cordova plugin add org.apache.cordova.file-transfer
    $ cordova plugin ls
    [ 'org.apache.cordova.file',
      'org.apache.cordova.file-transfer' ]
    $ cordova plugin rm org.apache.cordova.file-transfer

function gotFS(fileSystem) is not calling after onDeviceReady() function.

Here's the code I'm using:

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

This code is working for android. But for Ios, I am getting ReferenceError: Can't find variable: LocalFileSystem in this line -

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
Share Improve this question edited Jan 22, 2014 at 14:16 sudam asked Jan 22, 2014 at 12:41 sudamsudam 2113 silver badges10 bronze badges 7
  • Do you actually listen to the deviceready event to call the onDeviceReady function? Did you try to add console.log at each step to be sure what's called or not? – QuickFix Commented Jan 22, 2014 at 13:17
  • yes deviceready event is calling . this line have some problem window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); – sudam Commented Jan 22, 2014 at 14:00
  • This code is working for android. But for Ios, I am getting ReferenceError: Can't find variable: LocalFileSystem in this line - window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); – sudam Commented Jan 22, 2014 at 14:14
  • lines you show about plugin installation contain "cordova plugin rm", I hope you didn't run it. also how are you launching your project? – QuickFix Commented Jan 22, 2014 at 14:18
  • yes, I did not run cordova plugin rm.As rm for remove so didn't run that line. I am lunching project to device(ipad). – sudam Commented Jan 22, 2014 at 14:21
 |  Show 2 more ments

3 Answers 3

Reset to default 2

If LocalFileSystem isn't defined, it almost certainly means that the plugin's JavaScript code isn't being loaded.

Are you using any other Cordova APIs? Can you tell if cordova.js is being loaded from your HTML page and whether it runs correctly?

On iOS, one of the best debugging techniques for a problem like this is to connect to the iPad (or simulator) with Safari, and run

location.reload()

from the JavaSCript console. If cordova.js encounters an error, then it may stop running before it gets to loading the File plugin.

(FWIW, LocalFileSystem was never supposed to be a real object; it's actually an interface, which window is supposed to implement. I would switch to using window.PERSISTENT for patibility with the File API spec. That said, Cordova (for backwards patibility) should be setting the PERSISTENT and TEMPORARY symbols on both window and LocalFileSystem.)

Phonegap 3.3.0's Filesystem has a new approach. If you have been using fullpath for entry, you need to replace that with toURL().

Also in your config.xml file you got to add

<preference name="iosPersistentFileLocation" value="Compatibility" />

Your best bet would be to go over this link https://github./apache/cordova-plugin-file/blob/master/doc/index.md

Making these changes worked for me. Hope it works for you too.

Beside cordova. I also install phonegap like bellow mand. and make new application and install all plugin. Now file read write program is working. Thank You for help .

$ sudo npm install -g phonegap 
$ phonegap create my-app 
$ cd my-app 
发布评论

评论列表(0)

  1. 暂无评论