How to read csv file in Angular-4 assets

后端 未结 2 1302
温柔的废话
温柔的废话 2021-02-10 13:55

I am new to angular-4 and I want to read a csv file from angular-4 assets directory, file size 5mb and I dont\'t want to read this file from django back-end server because this

相关标签:
2条回答
  • 2021-02-10 14:23

    In case you use the new HttpClient class (@angular/common/http), then you will have to set the responseType to 'text', otherwise the HttpClient class would try to interpret the content as json and raise a SyntaxError...

    e.g.:
    this.http.get('assets/file.csv', {responseType: 'text'})
        .subscribe(
            data => {
                console.log(data);
            },
            error => {
                console.log(error);
            }
        );
    
    0 讨论(0)
  • 2021-02-10 14:41

    I finally find a answer Here is how I done it:

    csvUrl = 'assets/demo-Results.csv';
    readCsvData () {
          this.http.get(this.csvUrl)
            .subscribe(
              data => {
                console.log(data.text())
              },
              err => {
                console.log(err)
              });
      }
    
    0 讨论(0)
提交回复
热议问题