Angular 4:如何打开CSV本地文件? [英] Angular 4: How to open CSV local file?

查看:205
本文介绍了Angular 4:如何打开CSV本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Angular 4打开CSV文件.我在网络上找到了一个示例,但这并不是我真正需要的示例,因为在这种情况下,该文件是从服务器下载的.我需要在本地打开文件,可以吗?

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?

我找到了这个例子:

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

谢谢

推荐答案

您可以使用Java脚本 FileReader 对象和(确实是Angular 2 +).

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

这篇关于Angular 4:如何打开CSV本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆