Next.js Discord

Discord Forum

Regarding file reader, whats the most accurate way of reading a file?

Unanswered
<Milind ツ /> posted this in #help-forum
Open in Discord
When using file reader, should u declare readAstext before load or after load? I am trying to load a json file and parse its content and this is my code:

      const reader = new FileReader();
      reader.readAsText(file);
      reader.onprogress = (e) => {
         setLoaded(e.loaded);
      };
      reader.onload = () => {
         if (!reader.result) {
            showPickerToast(404, "No data");
            return;
         }

         const json = JSON.parse(reader.result.toString());
         setFileContent(json);
      };
      reader.onloadend = () => {
         reader.abort();
      };

while this works, please suggest if i am doing something wrong

9 Replies

Out if curosity
Why not do
JSON.parse(fs.readFileSync("./foo.json"))
because the file could be anywhere in the system. I am using react-dropzone to pick a file
Oh i didnt realize this was client side
sorry
wait no nvm u meant something else
my only doubt was regarding reader.readAsText()
and if i am doing anything wrong there