简体   繁体   English

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

[英]PhoneGap + AngularJS failed to read file that is just created

I'm using AngularJS in a PhoneGap 3.5.0 project and I use it on iOS 5 and 6 from XCode. 我在PhoneGap 3.5.0项目中使用AngularJS,并在XCode的iOS 5和iOS 6上使用了它。

I try to read a file that I write previously. 我尝试读取我之前编写的文件。 But I've always an empty string instead of the value of response which is a JSON. 但是我总是一个空字符串,而不是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);
                });
            });
        });
    });

});

I have this working version which works with photos: 我有这个适用于照片的工作版本:

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

the function that handles fail is 处理失败的功能是

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

I cannot see too much difference in why mine works and your does not, but try using the fail function to drill down to the possible error code and cause particular to your case. 我看不出我的工作原理与您的工作原理之间的太大差异,但是尝试使用fail函数向下钻取可能的错误代码,并根据您的情况进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM