简体   繁体   English

Angular 4:如何打开CSV本地文件?

[英]Angular 4: How to open CSV local file?

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. 我想知道如何使用Angular 4打开CSV文件。我在网络上找到了一个示例,但这并不是我所需要的,因为在这种情况下,该文件是从服务器下载的。 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 http://blog.sodhanalibrary.com/2016/10/read-csv-data-using-angular-2.html#.WYNdCnWGNp8

Thanks 谢谢

you can load files from the client machine using Javascript FileReader object and (Angular 2+ indeed). 您可以使用Javascript FileReader对象和(确实是Angular 2+)从客户端计算机加载文件。

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

you can use FileReader() api.have a look at this thread : Getting Data from FileReader in Angular 2 您可以使用FileReader()api。看看这个线程: 在Angular 2中从FileReader获取数据

Yes, it is possible like described in the link you provided too You implement a webservice that will provide you the local file . 是的,也有可能像您提供的链接中所描述的那样实现您将实现的Web服务,该服务将为您提供本地文件。 your angular applications makes a request to the webservice and retrieve the local file. 您的角度应用程序向Web服务发出请求并检索本地文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM