How to create csv file in android app with ionic 1

Hello everyone.

I am quite new in Angular JS 1 and are having difficulties in save CSV archieves in mobile device. I had researched a lot about some way to do that and found nothing. I though that maybe i could use cordova code to do so, but with my lack of knowledge, i ain’t able to do that.

Could someone help me please? Thank you everyone.

Thank you thesourav, i will read.

My friend thesourav.

I started to use the cordova-plugin-file tutorial and are having some little difficulties with the code passed from you.
After a lot of tests, i was able to download the plugin and see him working in my cellphone. I used the tutorial below to do so:

I don’t have problems to run the code in my cellphone and see the folder’s that he should return, but when started to try the code found in the stack overflow and in the web i ain’t able to write a file in my samsung. above, follow the code that i am trying to use:

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

Don’t work when it come in ‘function writelog(str)’, i think it is because the fileWriter. I already put the right permissions in my Android Manifest, to write and read file, but i still wasn’t able to solve my problem.

If you or anyone could help me, i would stay quite grateful.

What errors are you getting inside the function
You can use requestExternalStorageAuthorization() method from cordova diagnostic plugin to check if you have proper authorizations.

My friend. While i was waiting for your response i found a way to save archieves in my mobile device, i just have one problem now, with, i think, need a little more experience with javascript.

follow the code that i found:

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

This is simple and save a text Archive in my mobile device. I tried to use another code to create my csv arch, follow bellow:

    $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';
        }
        return str;
    }

The code above create my csv archive, but a ain’t able to pass that archive from the first code, making it saved in my mobile.
If you or anyone could help me, i again would be very grateful. Thank you again.