2

I just want to create a file. But as it turns out, this is quite difficult. Also, the official documentation of Apache is for JavaScript. However, i would like to implement it TypeScript.Let's just say I want to create an "access.log" file. But I'm not sure where the best place for this file is.

Many thanks for your help!

1 Answer 1

2

You can use the ionic native File API and create file using createFile function of the File API.

I would recommend storing "access.log" file in cordova.file.dataDirectory as it just one file and would guess it is for internal usage of app. As you have not mentioned specific usage of the file I would suggest you check out Where To Store Files section of cordova documentation ( The Ionic File API is just a wrapper around it, so the docs are same ).

Example code:

import { Component } from '@angular/core';
import { File } from '@ionic-native/file';

@Component({
    selector: 'demo-comp',
    templateUrl: 'demo-comp.component.html',
})
export class DemoCompComponent {
    constructor(private file: File) { }

    createAccessLogFileAndWrite(text: string) {
        this.file.checkFile(this.file.dataDirectory, 'access.log')
        .then(doesExist => {
            console.log("doesExist : " + doesExist);
            return this.writeToAccessLogFile(text);
        }).catch(err => {
            return this.file.createFile(this.file.dataDirectory, 'access.log', false)
                .then(FileEntry => this.writeToAccessLogFile(text))
                .catch(err => console.log('Couldn't create file));
        });
    }

    writeToAccessLogFile(text: string) {
        this.file.writeExistingFile(this.file.dataDirectory, 'access.log', text)
    }

    someEventFunc() {
      // This is an example usage of the above functions
      // This function is your code where you want to write to access.log file
      this.createAccessLogFileAndWrite("Hello World - someEventFunc was called");
    }
}

This is a demo you need to call createAccessLogFileAndWrite from your own function and pass the text you want to append to the file.

Let me know in the comments if you face any problems.

Sign up to request clarification or add additional context in comments.

10 Comments

Can you give me a small example how to do it ? I have read, I would have to prepare the file system first. I am a bit confused by these documents. I'm not doing so much with TypeScript or Web in general.
It seems like its not working. I dont get any error msg but no file was created. Here's my code: paste.ee/p/SaIWU (ts)
sorry, my bad. it should have been writeExistingFile, I have edit my code now, can u pls try again.
14:24:14] typescript: src/pages/home/home.ts, line: 27 Supplied parameters do not match any signature of call target. L26: if(!doesExist) { L27: return this.file.createFile(this.file.dataDirectory, 'Datei.txt') L28: .then(FileEntry => this.writeToFile(text)) [14:24:14] typescript: src/pages/home/home.ts, line: 28 Property 'writeToFile' does not exist on type 'HomePage'.
L27: return this.file.createFile(this.file.dataDirectory, 'Datei.txt') L28: .then(FileEntry => this.writeToFile(text)) L29: .catch(err => console.log('Couldnt create file')); [14:24:14] typescript: src/pages/home/home.ts, line: 36 Property 'writeExistingFile' does not exist on type 'HomePage'. L35: writeToAccessLogFile(text: string) { L36: this.writeExistingFile(this.file.dataDirectory, 'Datei.txt', text)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.