【问题标题】:Ionic Framework create file using File APIIonic Framework 使用 File API 创建文件
【发布时间】:2017-11-29 02:31:05
【问题描述】:

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

非常感谢您的帮助!

【问题讨论】:

    标签: angular api file typescript ionic-framework


    【解决方案1】:

    您可以使用 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 中告诉我。

    【讨论】:

    • 你能给我一个小例子吗?我已阅读,我必须先准备文件系统。我对这些文件有点困惑。一般来说,我对 TypeScript 或 Web 的操作并不多。
    • 它似乎不起作用。我没有收到任何错误消息,但没有创建文件。这是我的代码:paste.ee/p/SaIWU (ts)
    • 对不起,我的错。应该是writeExistingFile,我现在已经编辑了我的代码,你可以再试一次吗。
    • 14:24:14] typescript: src/pages/home/home.ts, line: 27 提供的参数与调用目标的任何签名都不匹配。 L26: if(!doesExist) { L27: return this.file.createFile(this.file.dataDirectory, 'Datei.txt') L28: .then(FileEntry => this.writeToFile(text)) [14:24:14 ] 打字稿:src/pages/home/home.ts,行:28 属性 'writeToFile' 在类型 'HomePage' 上不存在。
    • L27: 返回 this.file.createFile(this.file.dataDirectory, 'Datei.txt') L28: .then(FileEntry => this.writeToFile(text)) L29: .catch(err => console.log('无法创建文件')); [14:24:14] 打字稿:src/pages/home/home.ts,行:36 属性“writeExistingFile”在“主页”类型上不存在。 L35: writeToAccessLogFile(text: string) { L36: this.writeExistingFile(this.file.dataDirectory, 'Datei.txt', text)
    猜你喜欢
    • 2018-07-16
    • 2020-05-06
    • 1970-01-01
    • 2017-12-15
    • 2018-01-25
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多