Блог Фрилансера
Nov 25, 2020
![Csv file local to json](/uploads/images/Без названия.png)
Reading a file can be useful for previewing or pre-processing.
You can use FileReader to read the file in js. The following is an example of how to read csv downloaded through a browser
The function accepts input of type file
function readFile(input) {
let file = input.files[0];
let reader = new FileReader();
let readerText = new FileReader();
reader.readAsArrayBuffer(file);
readerText.readAsText(file); // is text
reader.onload = function() {
var resultUtf8is1251 = new TextDecoder('windows-1251').decode(reader.result);// here we change the encoding if necessary
console.log(resultUtf8is1251)
};
reader.onerror = function() {
console.log(reader.error);
};
}
You can see an example of use