PhoneGap + AngularJS无法读取刚刚创建的文件

时间:2014-09-24 22:30:27

标签: javascript angularjs cordova

我在PhoneGap 3.5.0项目中使用AngularJS,我在XCode的iOS 5和6上使用它。

我尝试读取我之前写的文件。但我总是一个空字符串而不是response的值,这是一个JSON。

$http.get(API + '/content')
.success(function (response) {
    // response is a JSON
    console.log(response);

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (filesystem) {
        filesystem.root.getDirectory('json', {create: true}, function (dirEntry) {
            dirEntry.getFile('contents.json', {create: true, exclusive: false}, function (fileEntry) {
                fileEntry.createWriter(function (fileWriter) {
                    fileWriter.onwriteend = function (e) {
                        // this is not working...
                        fileEntry.file(function (file) {
                            var reader = new FileReader();

                            reader.onloadend = function (e) {
                                // e.target.result is an empty string (!?)
                                console.log(e.target.result, typeof e.target.result);
                            };

                            reader.readAsText(file);
                        });
                    };

                    fileWriter.write(response);
                });
            });
        });
    });

});

1 个答案:

答案 0 :(得分:0)

我有这个适用于照片的工作版本:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
                fileSystem.root.getDirectory("photos", {create: true, exclusive: false}, function(dir) {
                    dir.getFile("photo_"+Date.now()+".jpg", {create: true, exclusive: false}, function(fileEntry){
                        fileEntry.createWriter(function(writer){
                            writer.onwriteend = function (evt) {
                                scope.processPhoto(fileEntry.toURL(), $(element).index());
                            };
                            writer.write(element.find('input')[0].files[0]);
                        }, fail);
                    }, fail);
                }, fail);
            }, fail);

处理失败的功能是

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

我看不出为什么我的工作和你的工作没有太大区别,但是尝试使用失败功能深入查看可能的错误代码并特别针对你的情况。