0

I would like to know how to open a CSV file using Angular 4. I found an example on the network, but it is not exactly what I need because in that case the file is downloaded from the server. I need to open the file locally, is it possible?

I found this example:

http://blog.sodhanalibrary.com/2016/10/read-csv-data-using-angular-2.html#.WYNdCnWGNp8

Thanks

Luis
  • 2,006
  • 5
  • 31
  • 47
  • If you mean opening a local (client-side) file from in the web browser see this question: https://stackoverflow.com/questions/3582671/how-to-open-a-local-disk-file-with-javascript – Brian Walker Aug 03 '17 at 21:48

2 Answers2

1

you can load files from the client machine using Javascript FileReader object and (Angular 2+ indeed).

import { Component } from '@angular/core';

@Component({
  selector: 'app',
  encapsulation: ViewEncapsulation.None,
  template: `<input id="preview" type="file" (change)="previewFile($event)" />`
})
export class AppComponent implements OnInit {
  ...
  public previewImage(event) {
    const reader = new FileReader();
    reader.onload = (e: any) => {
      console.log('csv content', e.target.result);
    };
    reader.readAsDataURL(event.target.files[0]);
  }
}
Anas Al Hamdan
  • 768
  • 1
  • 5
  • 18
0

you can use FileReader() api.have a look at this thread : Getting Data from FileReader in Angular 2

Yes, it is possible like described in the link you provided too You implement a webservice that will provide you the local file . your angular applications makes a request to the webservice and retrieve the local file.

sancelot
  • 1,905
  • 12
  • 31