- Content was generated with AI.
Learn more about Bing search resultshow Bing delivers search resultsLike Dislike
- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
You can read files from the root folder in JavaScript either by referencing them with a relative path or by letting the user select them via an HTML file input and using the FileReader API.
Method 1: Accessing via Relative Path
Place your JavaScript file in its folder (e.g.,./JS/main.js) and the target file in the root folder (e.g.,./img/img1.png).
Use..in the file path to go up one directory level until you reach the root folder.
Reference the file path relative to your HTML document or JavaScript file, for example:../../img/img1.png.
Method 2: Reading via File Input and FileReader API
Add anelement to your HTML to let the user select a file.
In JavaScript, get the file from the input usingdocument.getElementById('fileInput').files[0].
Create aFileReaderobject withlet reader = new FileReader();.
Attach anonloadevent to handle the file content once loaded.
Usereader.readAsText(file)to read text files,reader.readAsArrayBuffer(file)for binary data, orreader.readAsDataURL(file)for images.
Process or display the file content as needed in theonloadcallback.
Method 3: Using FileSystemDirectoryEntry.getFile()
Obtain aFileSystemDirectoryEntryobject for the root or target directory.
Call.getFile('filename', {}, successCallback, errorCallback)to get aFileSystemFileEntry.
InsidesuccessCallback, call.file()to get aFileobject.
Use aFileReaderto read the file contents as text, binary, or data URL.
How to read a local text file using JavaScript? - GeeksforGeeks
Jul 12, 2025· JavaScript can read local files using the File API, which is supported by most modern browsers. The File API allows JavaScript to read the contents of files chosen by the user, typically …
See results only from geeksforgeeks.orgSign In
JavaScript can read local files using the File API, which is supported by most modern browsers. The File API allows JavaScript to read the contents of files c…
javascript - How can I read a local text file in the browser? - Stack ...
Yes, JavaScript can read local files (see FileReader ()), but not automatically: the user has to pass the file or a list of files to the script with an HTML tag, .
Usage examplereadTextFile("file:http://test.test");How to Read and Write Files in JavaScript: Step-by-Step Tutorial with ...
Jan 16, 2026· How to Read and Write Files in JavaScript: Step-by-Step Tutorial with Sample Code Examples File handling—reading from and writing to files—is a fundamental skill in programming, …
How to Read Text File in JavaScript - Delft Stack
Mar 11, 2025· This tutorial demonstrates how to read text files in JavaScript, covering methods for both the browser and Node.js environments. Learn to use the File API, Fetch API, and Node.js fs module …
How to Read a Local Text File in JavaScript: JS …
Aug 20, 2025· This is a simple approach to reading files in JavaScript using the FileReader API and its four methods. Whether you are programming in the …
FileReader - Web APIs | MDN - MDN Web Docs
Jun 23, 2025· The FileReader interface lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or …
Read files in JavaScript | Articles | web.dev
Jun 18, 2010· This example reads a File provided by the user, then converts it to a data URL, and uses that data URL to display the image in an img element. To learn how to verify that the user has …
JavaScript FileReader
In this tutorial, you'll learn about the JavaScript FileReader API and how to use it to implement the file upload.
JavaScript File and FileReader - W3docs
JavaScript, which is key to client-side scripting, provides strong tools through the File and FileReader interfaces. This guide offers a detailed look at these tools, giving developers the skills to handle files …
How to Handle Files in JavaScript: Reading, Uploading, and Converting
Nov 14, 2024· This article provides a comprehensive guide to file handling in JavaScript. It covers file selection, retrieving file properties, and uploading files to a server. Key concepts such as the …