Create file and write data in it

Hi All,

Can Any one help me please ?? I want to use cordova plugin file writer in ionic app, I have read these topics
Cordova-plugin-file File writer help
How to write data to a file using ngCordova?

but kindly, I need a complete example

Thanks in advance

Kindly, can you show what you have so far?

Thank you for your response @aaronksaunders

I want a simple example about how to use
$cordovaFile.createFile
$cordovaFile.writeFile
in ionic

I use the code written here How to write data to a file using ngCordova? as following

in app.js

example.controller('TestCtrl', ['$scope', '$q', '$cordovaFile'], function ($scope, $q, $cordovaFile) {
        console.log('TestCtrl');

        function ClearDirectory() {
            console.log('ClearDirectory');
            $cordovaFile.listDir(fileDir + 'test').then(function (entries) {
                console.log('listDir: ', entries);
            }, function (err) {
                console.error('listDir error: ', err);
            });
            $cordovaFile.removeRecursively(fileDir + 'test')
                .then(function () {
                    console.log(trinlDir + ' recursively removed');
                },
                function (err) {
                    console.log(fileDir + ' error: ', err);
                });
        }

        function testFS() {
            // Download file from 'http://www.yourdomain.com/test.jpg' to test/one/test.jpg on device Filesystem
            var hostPath = 'http://www.yourdomain.com/test.jpg';
            var clientPath = fileTransferDir + 'test/one/test.jpg';
            var fileTransferOptions = {};

            $cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions).then(function () {
            });
            // Create dir test
            $cordovaFile.createDir(fileDir + 'test/').then(function (dirEntry) {
            });
            // Create dir aganin in dir test
            $cordovaFile.createDir(fileDir + 'test/one/').then(function (dirEntry) {
            });
            // Create empty file test.txt in test/again/
            $cordovaFile.createFile(fileDir + 'test/one/test.txt', true).then(function (fileEntry) {
            });
            // List of files in test/again
            $cordovaFile.listDir(fileDir + 'test/one/').then(function (entries) {
                console.log('list dir: ', entries);
            });
            // Write some text into file
            $cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', '').then(function (result) {
            });
            // Read text written in file
            $cordovaFile.readAsText(fileDir + 'test/one/test.txt').then(function (result) {
                console.log('readAsText: ', result);
            });
        }

        function testQ() {
            var hostPath = 'http://www.yourdomain.com/test.jpg';
            var clientPath = fileTransferDir + 'test/one/test.jpg';
            var fileTransferOptions = {};
            $q.all([
                    $cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions),
                    $cordovaFile.createDir(fileDir + 'test/'),
                    $cordovaFile.createDir(fileDir + 'test/two/'),
                    $cordovaFile.createFile(fileDir + 'test/one/test.txt', true),
                    $cordovaFile.listDir(fileDir + 'test/one/'),
                    $cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', ''),
                    $cordovaFile.readAsText(fileDir + 'test/one/test.txt')
                ]).then(function (result) {
                console.log('testQ result: ', result);
            });
        }
    });

in index.html

<ion-content ng-controller="TestCtrl">
          <button class="button" ng-click=ClearDirectory() ></button>
          <button class="button" ng-click=testFS() ></button>
      </ion-content>

but I don’t know where is the file ? 'test/one/test.txt’
I test it on my android and I did not find anything :frowning:

I’d like a concrete example too! I’m kinda stuck with the file creation and appending on android. The documentation for the file plugin does not contain examples. The ngcordova doc does not help very much either.

1 Like