Home >Web Front-end >JS Tutorial >How Can JavaScript Access and Process Local Files?
Accessing Local Disk Files in JavaScript
In web development, opening local files presents security challenges that prevent direct access via methods like window.open(). However, there are alternative approaches that allow JavaScript to read and process local data.
FileReader API
The FileReader API provides a way to read files asynchronously. Here's an example:
function readSingleFile(e) { var file = e.target.files[0]; if (!file) { return; } var reader = new FileReader(); reader.onload = function(e) { var contents = e.target.result; displayContents(contents); }; reader.readAsText(file); }
In this example, a listener is attached to a element. When a file is selected, the readSingleFile function is triggered, using FileReader.readAsText() to read the file as plain text. The result of the operation is passed to the displayContents function for display.
The above is the detailed content of How Can JavaScript Access and Process Local Files?. For more information, please follow other related articles on the PHP Chinese website!