【问题标题】:How to read a text file that was downloaded locally (Ionic 2 and Cordova)如何读取本地下载的文本文件(Ionic 2 和 Cordova)
【发布时间】:2016-12-24 05:24:19
【问题描述】:

我使用以下代码从我的网络服务器下载了一个文件:

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 native 是最好的,但我该怎么做呢?如果有人可以提供一个例子,我将不胜感激。

如果有人对我想要达到的目标有任何具体问题,我会进行编辑。

谢谢!

【问题讨论】:

    标签: cordova typescript ionic2


    【解决方案1】:

    我使用 FileChooser 打开并读取文件。也许这可以给你一些想法。

    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));
    }

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 1970-01-01
      • 2016-01-31
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多