【问题标题】:Ionic 3 get contents of an asset fileIonic 3 获取资产文件的内容
【发布时间】:2017-11-19 23:01:57
【问题描述】:

我正在构建一个应用程序来显示一本书的内容。我在这个应用中有 3 个页面:

第 1 页。链接到章节的按钮列表; (ChaptersPage)
Page 2. 链接到章节页面的按钮列表; (PagesPage)
Page 3. 页面内容; (PageContentPage)

第 1 页和第 2 页在一个选项卡内,因此当用户选择一个章节时,他会转到下一个选项卡,然后选择该章节的一个页面,然后获取页面内容。选项卡将如下所示:

所有页面和章节都在他自己的文件夹中。我的应用文件夹如下所示:

src
|_ app
|_ assets
   |_ icon
   |_ images
   |_ chapters
      |_ 1
         |_page_1.txt
         |_page_2.txt
      |_ 2
         |_page_1.txt
         |_page_2.txt
      |_ 3
         |_page_1.txt
         |_page_2.txt
      |_  ...
|_ pages
|_ services
|_ ...

首先,当用户选择章节时,我需要查看章节文件夹中有多少页,以便列出正确数量的按钮。我正在尝试使用ionic native File plugin,但没有成功。使用文件插件文档中的示例,我总是得到“目录不存在”:

this.file.checkDir(this.file.applicationDirectory, 'assets/chapters/2').then(_ => 
   console.log('Directory exists')
).catch(err => console.log('Directory doesnt exist'));

所以,我的问题是:如何读取章节文件夹中有多少文件?之后,如何获取 page.txt 的内容?

【问题讨论】:

    标签: angular typescript ionic-framework ionic3


    【解决方案1】:
    readPage() {
        this.http.request('../assets/chapters/1/page_1.txt')
              .map(res => res.text())
              .subscribe(text => {
                this.txtContent= text;
              })
              .catch(error => {
                console.err("Path not found with error:", error);
              });
    }
    

    【讨论】:

    • .map(res => res.text() }) 看起来不对它有一个额外的}
    • 已修复。不错的捕获@Whitecat。
    【解决方案2】:

    首先更改此代码块,这会将您带到您想要的目录

    this.file.checkDir(this.file.applicationDirectory, './chapters/2').then(_ => 
       console.log('Directory exists')
    ).catch(err => console.log('Directory doesnt exist'));
    

    【讨论】:

    • 在当前的 ionic 版本中不能这样工作吗?我在资产中放置了一个名为“img”的文件夹,使用 this.file.checkFile 无法检测到它
    【解决方案3】:
    • 请尝试以下安卓设备:
    this.file.checkDir(this.file.applicationDirectory,'public/assets/chapters/2')
    .then(_ => 
       console.log('Directory exists')
    ).catch(err => console.log('Directory doesnt exist'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-05
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      相关资源
      最近更新 更多