我无法在cordova-plugin-file

时间:2017-04-15 16:36:27

标签: javascript android angularjs cordova ionic-framework

我是AngularJS和cordova的新手,并且在解决简单问题方面遇到一些小问题。 我正在研究Cordova-Plugin-File,并且无法在我的AngularJS移动设备应用程序中运行它。有人能帮助我吗?

遵循我从网络中的示例中获取的代码。

的javascript:

    var logOb = "";
    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
        console.log("got main dir", dir);
        dir.getFile("log.txt", { create: true }, function (file) {
            console.log("got the file", file);
            logOb = file;
            writelog("App started");
        });
    });

    function writelog(str) {
        if (!logOb) alert('teste2');
        var log = str + " [" + (new Date()) + "]\n";
        console.log("going to log " + log);
        logOb.createWriter(function (fileWriter) {

            fileWriter.seek(fileWriter.length);

            var blob = new Blob([log], { type: 'text/plain' });
            fileWriter.write(blob);
            console.log("ok, in theory i worked");
        }, fail);
    }

记住我已经测试了插件并且它运行良好。我用这个教程来测试它:

https://www.youtube.com/watch?v=bizW561ALsY

命令会根据需要返回手机中的文件夹。当我进入'createWriter'命令时,我只是对上面的例子有问题,并且无法从中传递。

如果有人能帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:0)

更新

当我在等待回应时,我发现了一些方法可以开始做我需要的事情。首先,我能够创建档案.csv。

$scope.criaCSV = function () {
    // Create Object

    var items = [
        { name: "Item 1", color: "Green", size: "X-Large" },
        { name: "Item 2", color: "Green", size: "X-Large" },
        { name: "Item 3", color: "Green", size: "X-Large" }];

    // Convert Object to JSON
    var jsonObject = JSON.stringify(items);

    // Convert JSON to CSV & Display CSV
    ConvertToCSV(jsonObject);
 };

function ConvertToCSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';

    for (var i = 0; i < array.length; i++) {
        var line = '';
        for (var index in array[i]) {
            if (line != '') line += ','
            line += array[i][index];
        }
        str += line + '\r\n';
    }

后者我能够在我的手机中保存一个txt archieve:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);
}

function onSuccess(fileSystem) {
    alert(fileSystem);
    var directoryEntry = fileSystem.root;

    //lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
    directoryEntry.getFile("readme.txt", { create: true, exclusive: false }, function (fileEntry) {
        //lets write something into the file
        fileEntry.createWriter(function (writer) {
            alert("This is the text inside readme file");
        }, function (error) {
            alert("Error occurred while writing to file. Error code is: " + error.code);
        });
    }, function (error) {
        alert("Error occurred while getting a pointer to file. Error code is: " + error.code);
    });
}

function onError(evt) {
    alert("Error occurred during request to file system pointer. Error code is: " + evt.code);
}

有人会帮我把那个拱门传递给我保存txt的代码,改变那个拱门的txt吗?

谢谢大家。