How to upload a CSV file and read them using angular2?

前端 未结 4 1022
野趣味
野趣味 2020-12-15 05:49

i have a requirement of uploading a .CSV file and read them inside my component, i have gone through this blog but it has a .CSV file stored in a particular loaction, i want

4条回答
  •  一整个雨季
    2020-12-15 06:30

    I made a upload functionality on my app. Hope it helps

    Here's my sample upload function inside my component

    uploadDatasource(fileInput: any) {
    
    let file = fileInput.target.files[0];
    let fileName = file.name;
    
    
    let payload = {
      file,
    }
    
    let formData: FormData = new FormData();
    formData.append('file',file,file.name);
    
    
    this.DsListService.uploadDatasource(formData)
      .subscribe(
        response => { 
          console.log('UPLOADING success');
    
    
        },
        error => {
          console.log('error',error)
        });
    
    }
    

    here's my service class

    import { Injectable } from '@angular/core';
    import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http';
    import { Observable } from 'rxjs/Rx';
    import { Config } from '../config/config';
    
    @Injectable()
    export class DsListService {
    
      private config = new Config;
    
      constructor(private http: Http) { }
    
    
      uploadDatasource(payload): Observable {
        let headers = new Headers();
    
        headers.append('Accept', 'application/json, text/plain,');
        let options = new RequestOptions({ headers: headers });
    
    
        return this.http.post(`API_UPLOAD_PATH`,payload, options)
          .map((res: Response) => {
            let data = res.json();
            return data;
          })
          .catch(error => Observable.throw(error))
    
      }
    }
    

    and here's my html

    
    

提交回复
热议问题