【问题标题】:PhoneGap + AngularJS failed to read file that is just createdPhoneGap + AngularJS 无法读取刚刚创建的文件
【发布时间】:2014-09-24 22:30:27
【问题描述】:

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

});

【问题讨论】:

    标签: javascript angularjs cordova


    【解决方案1】:

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

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

    我看不出为什么我的工作和你的工作没有太大区别,但请尝试使用失败功能深入了解可能的错误代码并导致你的情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      相关资源
      最近更新 更多