How to access a JSON File from Hard Disk

Hello,

my question is how can I access a file from Hard disk? Unfortunately, I can’t just copy the file to the project folder. I need the file for example from C:\path\to\file. I tried using fetch, but it only works IF my JSON file is in the same folder as the project itself e.g.

fetch("./file.json")
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((e) => console.log(e));

I also tried using required with node. But when I use required, it becomes a nodejs environment and I don’t have access to the HTML which I need. Do you guys have an idea or tips to the situation? Thanks.

There’s a FileSystem API in the browser but it’s not supported by all vendors (I think only Chrome and Edge do).

Otherwise you can have an file input select, let the user select the file then use the FileReader to read the selected file.

But overall reading from the file system reliably is a job for Node and fs:readFile.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.