Angular 4: How to open CSV local file?

久未见 提交于 2019-12-04 06:16:15

问题


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


回答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]);
  }
}



回答2:


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.



来源:https://stackoverflow.com/questions/45494691/angular-4-how-to-open-csv-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!