使用PhoneGap 3.3.0为ios访问文件 [英] Access files using using Phonegap 3.3.0 for ios

查看:206
本文介绍了使用PhoneGap 3.3.0为ios访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用IOS上的文件,使用Phonegap [cordova 3.3.0]。我读了如何访问文件和阅读他们的API文档的手机差距。也添加了此类插件

  $ cordova插件添加org.apache.cordova.file 
$ cordova插件ls
['org.apache.cordova.file']
$ cordova插件rm org.apache.cordova.file

$ cordova插件添加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

函数gotFS(fileSystem)不调用onDeviceReady()函数。 p>

这里是我使用的代码:

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

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

此代码适用于Android。但对于Ios,我得到ReferenceError:找不到变量:LocalFileSystem
在这一行 -

 窗口。 requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,fail); 


解决方案

Phonegap 3.3.0的文件系统有一个新的方法。如果您使用完整路径进行输入,则需要使用toURL()替换。



在您的config.xml文件中,您还需要添加

  < preference name =iosPersistentFileLocationvalue =Compatibility/> 

最好的办法是通过此链接 https://github.com/apache/cordova-plugin-file/blob/master/doc/index。 md



这些更改对我有用。希望它也为你工作。


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

解决方案

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.com/apache/cordova-plugin-file/blob/master/doc/index.md

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

这篇关于使用PhoneGap 3.3.0为ios访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆