繁体   English   中英

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

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

我在PhoneGap 3.5.0项目中使用AngularJS,并在XCode的iOS 5和iOS 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);
                });
            });
        });
    });

});

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

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

我看不出我的工作原理与您的工作原理之间的太大差异,但是尝试使用fail函数向下钻取可能的错误代码,并根据您的情况进行调整。

暂无
暂无

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

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