Angular Ionic框架使用文件API创建文件

Angular Ionic框架使用文件API创建文件,angular,api,file,typescript,ionic-framework,Angular,Api,File,Typescript,Ionic Framework,我只想创建一个文件。但事实证明,这是相当困难的。此外,Apache的官方文档是针对JavaScript的。但是,我想实现它的TypeScript。假设我想创建一个“access.log”文件。但是我不确定这个文件最好放在哪里 非常感谢你的帮助 您可以使用ionic native并使用file API的函数创建文件 我建议将“access.log”文件存储在cordova.file.dataDirectory中,因为它只有一个文件,并且可能是用于应用程序的内部使用。由于您没有提到该文件的具体用法,

我只想创建一个文件。但事实证明,这是相当困难的。此外,Apache的官方文档是针对JavaScript的。但是,我想实现它的TypeScript。假设我想创建一个“access.log”文件。但是我不确定这个文件最好放在哪里


非常感谢你的帮助

您可以使用ionic native并使用file API的函数创建文件

我建议将“access.log”文件存储在
cordova.file.dataDirectory
中,因为它只有一个文件,并且可能是用于应用程序的内部使用。由于您没有提到该文件的具体用法,我建议您查看cordova文档的部分(Ionic file API只是它的包装,所以文档是相同的)

示例代码:

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");
    }
}
这是一个演示,您需要从自己的函数调用
createAccessLogFileAndWrite
,并将要附加的文本传递到文件中


如果你遇到任何问题,请在评论中告诉我。

你能给我一个小例子吗?我已经读过了,我必须先准备好文件系统。我被这些文件弄糊涂了。一般来说,我对TypeScript或Web没有做太多的工作。它似乎不起作用。我没有收到任何错误消息,但没有创建任何文件。这是我的密码:(ts)对不起,我的错。它应该是
写的现有文件
,我现在已经编辑了我的代码,请再试一次。14:24:14]typescript:src/pages/home/home.ts,第27行提供的参数与调用目标的任何签名都不匹配。L26:如果(!doesExist){L27:返回this.file.createFile(this.file.dataDirectory,'Datei.txt')L28:。然后(FileEntry=>this.writeToFile(text))[14:24:14]typescript:src/pages/home/home.ts,第28行属性“writeToFile”在类型“HomePage”上不存在。L27:返回this.file.createFile(this.file.dataDirectory,'Datei.txt')L28:。然后(FileEntry=>this.writeToFile(text))L29:。catch(err=>console.log('Couldnt create file');[14:24:14]typescript:src/pages/home/home.ts,第36行属性“writeExistingFile”在类型“HomePage”上不存在。L35:writeToAccessLogFile(text:string){L36:this.writeExistingFile(this.file.dataDirectory,'Datei.txt',text)