0

So, in HTML i have this input with which I read a File.

<div class="form-group">
    <label for="file">Choose File</label>
    <input type="file" id="file"(change)="function($event.target.files)">
</div>

Now, if i upload a csv File, i can use it in the Code as a csv-file-object as far as i know.

My problem is, that I need all the data from the csv file as a string.

How can i convert the data from the file into a string?

J.Doe
  • 586
  • 2
  • 8
  • 30
  • 1
    Possible duplicate of [Reading file contents on the client-side in javascript in various browsers](https://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers) – Abbas Amiri Sep 05 '19 at 12:07
  • I want to read the file in angular, how could i manage that? – J.Doe Sep 05 '19 at 12:23
  • The argument passed to `function` is an array of files, just pick the first item of files and the rest is js (ts) code that is described in the aforementioned answer. It would be better to pass the `$event` to the `function` I believe. – Abbas Amiri Sep 05 '19 at 12:30

1 Answers1

1

It seems you want to convert a csv to a json-string. If so you can use the

npm-package csvjson

first install: npm i csvjson

use it:

var csvjson = require('csvjson');
var data = fs.readFileSync(path.join(__dirname, 'schema_sample2.csv'), { encoding : 'utf8'});

var options = {
  delimiter : ',', // optional
  quote     : '"' // optional
};



csvjson.toObject(data, options);
Micha
  • 906
  • 6
  • 9