The following code is suppose to import data into localStorage from a CSV file. The goal is that is should replace / update any existing localStorage data. I am using FileReader to import and was tryign to use JSON.stringify to save it to localStorage.
const fileInput = document.getElementById('csv');
const readFile = () => {
const reader = new FileReader();
reader.onload = () => {
localStorage.setItem("entry_list", JSON.stringify(reader.result));
}
reader.readAsArrayBuffer(fileInput.files[0]);
};
fileInput.addEventListener('change', readFile);
I believe the issues is that is it trying to import this as an OBJECT rather than an ARRAY. How do I approach this problem or fix this issue?
2
Answers
The issue is that you try to use
JSON.stringify
on anArrayBuffer
object. AnArrayBuffer
does not have any enumerable object properties and hence becomes just{}
when stringified.Either turn it into a regular array using
JSON.stringify(Array.from(new Uint8Array(reader.result)))
so you get every byte in the file as a number, or – my recommendation, since it’s easier to turn into a file again and smaller to store – don’t read it asArrayBuffer
in the first place but as data URI (which will use base64-encoding) by replacingreadAsArrayBuffer
withreadAsDataURL
.However, based on your comments, I am now quite sure that you would like to parse the CSV contents as well, instead of just storing the file. In this case take a look at one of the many CSV parsing packages out there, such as
csv-parse
.Here is an example how to use it:
Instead of a CDN like I used here, you would normally copy the relevant file from the package onto your server (or install the package there, if you can) and reference the file this way.
CSV is plaintext, importing an array buffer is for binary data/files. You can just use the fetch API to read all of the text and then parse it into objects from CSV using a module perhaps: