如何使用 Ionic 3 将文本文件的内容作为字符串获取?
How can I use Ionic 3 to get the content of a text file as a string?
我试过这个:
this.item = this.params.get('filename');
console.log("[INFO] Opening File: >" + this.item + "<");
this.textboxContent = this.file.readAsText(this.file.dataDirectory, this.item);
console.log("[INFO] Content of textboxContent: >" + this.textboxContent + "<");
但是我进入了logcat:
[INFO:CONSOLE(56865)] "[INFO] Content of textboxContent: >[object
Promise]<"
this.item 是:
[INFO:CONSOLE(56863)] "[INFO] Opening File: >SomeFile.txt<"
这是因为 readAsText returns 一个承诺。你应该这样做
this.file.readAsText(this.file.dataDirectory, this.item).then((content)=>
{
this.textboxContent = content;
})
我试过这个:
this.item = this.params.get('filename');
console.log("[INFO] Opening File: >" + this.item + "<");
this.textboxContent = this.file.readAsText(this.file.dataDirectory, this.item);
console.log("[INFO] Content of textboxContent: >" + this.textboxContent + "<");
但是我进入了logcat:
[INFO:CONSOLE(56865)] "[INFO] Content of textboxContent: >[object Promise]<"
this.item 是:
[INFO:CONSOLE(56863)] "[INFO] Opening File: >SomeFile.txt<"
这是因为 readAsText returns 一个承诺。你应该这样做
this.file.readAsText(this.file.dataDirectory, this.item).then((content)=>
{
this.textboxContent = content;
})