如何阅读本地下载的文本文件(Ionic 2和Cordova) [英] How to read a text file that was downloaded locally (Ionic 2 and Cordova)
问题描述
我使用以下代码从我的网络服务器下载了一个文件:
I've downloaded a file from my webserver using the following code:
download = () : void => {
this.platform.ready().then(() => {
let pathToSaveTo = "";
let filename = this.url.substring(this.url.lastIndexOf('/') + 1);
if (this.platform.is('android')) {
pathToSaveTo = cordova.file.dataDirectory + filename;
}
else if(this.platform.is('ios')) {
pathToSaveTo = cordova.file.dataDirectory + filename;
}
let ft = new Transfer();
ft.download(this.url, pathToSaveTo).then(() => {
this.savedTo = pathToSaveTo;
});
})
我在寻找开辟方法方面遇到了很多麻烦.txt文件并读取里面的文本。我已经看了很长一段时间了,似乎使用Ionic本机会是最好的,但我究竟会怎么做呢?如果有人可以提供一个例子我会非常感激。
I'm having a lot of trouble finding out a way to open the .txt file and read the text inside. I've looked for quite a while, and it seems like using Ionic native would be the best, but how exactly would I go about that? If someone could provide an example I would really appreciate it.
如果有人对我想要实现的目标有任何具体问题,我会进行编辑。
If anybody has any specific questions about what I'm trying to achieve I'll make edits.
谢谢!
推荐答案
我使用FileChooser打开并读取文件。也许这可以给你一些想法。
I use FileChooser open and read a file. Maybe this can give you some idea.
openFile(): void {
FileChooser.open()
.then(uri => {
File.resolveLocalFilesystemUrl(uri)
.then(entry=>{
let path = entry.nativeURL.substring(0, entry.nativeURL.lastIndexOf('/'));
File.readAsText(path, entry.name)
.then(content=>{
console.log(content);
})
.catch(err=>{
console.log(err);
});
})
})
.catch(e => console.log(e));
}
这篇关于如何阅读本地下载的文本文件(Ionic 2和Cordova)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!