Angular: How do I read a text file located in the assets folder?

Viewed 12251

I am trying to open a .txt file which is in my assets folder. The name is "Maze1.txt". I am sort of new to these things so I was wondering if someone can help me read the text from this file. Thank you so much. If there is any more information needed, please tell me. :)

2 Answers

You can use the HttpClient for this (import and inject it as httpClient).

this.httpClient.get('assets/Maze1.txt', {responseType: 'text'})
        .subscribe(data => console.log(data));

Nevermind. I found a working solution by using a constructor. For anyone looking for code that works, use this:

private httpClient: HttpClient;

  constructor(http: HttpClient) {
    this.httpClient = http;
  }

and in the function: I used this:

this.httpClient.get('assets/Maze1.txt', { responseType: 'text' })
      .subscribe(data => console.log(data));
Related