【发布时间】:2017-11-29 02:31:05
【问题描述】:
我只想创建一个文件。但事实证明,这是相当困难的。此外,Apache 的官方文档是针对 JavaScript 的。但是,我想实现它 TypeScript。假设我想创建一个“access.log”文件。但是我不确定这个文件的最佳位置在哪里。
非常感谢您的帮助!
【问题讨论】:
标签: angular api file typescript ionic-framework
我只想创建一个文件。但事实证明,这是相当困难的。此外,Apache 的官方文档是针对 JavaScript 的。但是,我想实现它 TypeScript。假设我想创建一个“access.log”文件。但是我不确定这个文件的最佳位置在哪里。
非常感谢您的帮助!
【问题讨论】:
标签: angular api file typescript ionic-framework
您可以使用 ionic native File API 并使用 File API 的 createFile 函数创建文件。
我建议将“access.log”文件存储在cordova.file.dataDirectory 中,因为它只是一个文件,并且猜测它是供应用程序内部使用的。由于您没有提到该文件的具体用法,我建议您查看cordova 文档的Where To Store Files 部分(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 并将您想要附加到文件的文本传递给该文件。
如果您遇到任何问题,请在 cmets 中告诉我。
【讨论】:
writeExistingFile,我现在已经编辑了我的代码,你可以再试一次吗。