Course contentsShow
← Web Development
Lesson 2693 of 2,98248. File Uploads and HandlingFree lesson

Accessing Read Results

Understanding the result property and how to access file data after successful reads.

Accessing Read Results

You'll learn how to retrieve and use the data stored in a FileReader's result property after a successful file read operation.

The Core Idea

When a FileReader finishes reading a file, it doesn't automatically hand you the data—you need to know where to look. The FileReader stores the processed file content in its result property, but this property only contains meaningful data after the read operation completes successfully.

Think of it like a photocopier: you press the button, wait for the machine to finish, and then retrieve your copy from the output tray. The result property is that output tray—empty until the job completes.

How It Works

The result property's content depends on which read method you used:

  • After readAsText(): contains a plain text string
  • After readAsDataURL(): contains a base64-encoded data URL string
  • After readAsArrayBuffer(): contains an ArrayBuffer object

You access result inside the onload event handler (or after the load event fires), because that's when you're guaranteed the read has finished:

const reader = new FileReader();

reader.onload = function(event) {
  // Access the result from the event target
  const fileContent = event.target.result;
  console.log(fileContent);
  
  // Or directly from the reader instance
  const sameContent = reader.result;
  console.log(sameContent);
};

reader.readAsText(file);

Both event.target.result and reader.result reference the same data—use whichever feels clearer in your code.

Key Takeaway: The FileReader's result property holds your file data after a successful read operation, accessible through either the event object or the reader instance within the onload handler.