Using the FileReader API to preview images in React - LogRocket Blog (2024)

Introduction

Images make up a significant proportion of data transmitted on the internet. More often than not, clients have to upload image files from their devices to the server. To ensure users upload image files of the correct type, quality, and size, most web applications have features for previewing images.

Using the FileReader API to preview images in React - LogRocket Blog (1)

In the browser environment, clients can initiate image upload by browsing files using an input element or the drag and drop API. You can then use the URL API or the FileReader API to read the image files and preview them.

Though previewing images with the URL API is straightforward, using the FileReader API can be daunting. Therefore, in this article, you will learn how to preview images in a React application with the FileReader API. We shall cover both single and batch image previews.

Contents

  • How to browse image files in React
  • Introduction to the FileReader API
  • How to preview single image before upload in React with the FileReader API
  • How to preview multiple images before upload in React with the FileReader API
  • Conclusion

How to browse image files in React

If you want to add file upload functionality to your web application, an input element of type file comes in handy. It enables users to select single or multiple files from storage in their computer or mobile device:

<input type="file" accept="image/*" multiple />

The above input element will look like a button when rendered by the browser. Clicking it will open the operating system’s built-in file chooser dialog. The user can then select the image files for upload.

The input element has the accept attribute for restricting the file type. Its value is a string consisting of file type specifiers separated by commas. The value of the accept attribute is image/* in the input element above. It enables us to browse and upload images of any format.

To upload image files of a specific format, you can restrict the value of the accept attribute. For example, setting its value to image/png or .png only accepts PNG images.

With the multiple boolean attribute set to true, a user can select multiple image files. On the other hand, a user can browse only one image file if its value is false. It is worth noting that a boolean attribute’s value is true if the attribute is present on an element, and false if omitted.

The browser emits the change event after a user completes the file selection. Therefore, you should listen for the change event on the input element. You can do it like so in React:

<form> <p> <label htmlFor="file">Upload images</label> <input type="file" id="file" onChange={changeHandler} accept="image/*" multiple /> </p></form>

In the change event handler, you can access the FileList object. It is an iterable whose entries are File objects. The File objects contain read-only metadata such as the file name, type, and size:

const changeHandler = (e) => { const { files } = e.target for (let i = 0; i < files.length; i++) { const file = files[i]; // OR const file = files.item(i); }}

Introduction to the FileReader API

The FileReader API provides an interface for asynchronously reading the contents of a file from a web application.

As highlighted in the previous section, you can use an input element of type file to browse files from a user’s computer or mobile device. Selecting image files this way returns a FileList object whose entries are File objects.

The FileReader API then uses the File object to asynchronously read the file the user has selected. It is worth mentioning that you can not use the FileReader API to read the contents of a file from the user’s file system using the file’s pathname.

The FileReader API has several asynchronous instance methods for performing read operations. These methods include:

  • readAsArrayBuffer
  • readAsBinaryString
  • readAsDataURL
  • readAsText

In this article, we shall use the readAsDataURL method. The readAsDataURL method takes the file object as an argument, and asynchronously reads the image file into memory as data URL.

It emits the change event after completing the read operation:

const fileReader = new FileReader();fileReader.onchange = (e) => { const { result } = e.target;}fileReader.readAsDataURL(fileObject);

You can read the documentation for a detailed explanation of the other FileReader instance methods.

Over 200k developers use LogRocket to create better digital experiencesLearn more →

How to preview single image before upload in React

In this section, we shall look at how to preview a single image before uploading in React with the FileReader API. It assumes you have a React project set up already.

The code below shows how to read and preview a single image in React with the FileReader API. We are using an input element of type file to browse image files. Because we want to preview a single image, I have omitted the multiple boolean attribute on the input element:

import { useEffect, useState } from 'react';const imageMimeType = /image\/(png|jpg|jpeg)/i;function App() { const [file, setFile] = useState(null); const [fileDataURL, setFileDataURL] = useState(null); const changeHandler = (e) => { const file = e.target.files[0]; if (!file.type.match(imageMimeType)) { alert("Image mime type is not valid"); return; } setFile(file); } useEffect(() => { let fileReader, isCancel = false; if (file) { fileReader = new FileReader(); fileReader.onload = (e) => { const { result } = e.target; if (result && !isCancel) { setFileDataURL(result) } } fileReader.readAsDataURL(file); } return () => { isCancel = true; if (fileReader && fileReader.readyState === 1) { fileReader.abort(); } } }, [file]); return ( <> <form> <p> <label htmlFor='image'> Browse images </label> <input type="file" id='image' accept='.png, .jpg, .jpeg' onChange={changeHandler} /> </p> <p> <input type="submit" label="Upload" /> </p> </form> {fileDataURL ? <p className="img-preview-wrapper"> { <img src={fileDataURL} alt="preview" /> } </p> : null} </> );}export default App;

As illustrated in the above example, you can listen for the change event on the input element. The change event handler is invoked after a client completes the file selection. You can access the File object representing the selected file and update state in the event handler.

Since the HTML markup in the browser is editable, it is necessary to check the MIME type of the selected file before starting the read process. Though it is unlikely that an ordinary user would edit the HTML elements on a web page, it prevents anyone from easily breaking your app.

After uploading your files, you will have to do a similar check on the server side. At this point, you can also check the size of the selected file to make sure it does not exceed a maximum limit.

Since reading the selected file is a side effect, we use the useEffect hook. As highlighted in the previous section, you start by creating an instance of FileReader. The readAsDataURL method of the FileReader API reads the file asynchronously and emits the load event after completing the reading process.

It is possible for the component to unmount or rerender before completing the read process. You will need to abort before unmounting if the read process is incomplete. To prevent memory leaks, React disallows state updates after unmounting a component. Therefore, we need to check whether the component is still mounted before updating state in the load event handler.

We access the file’s data as a base64-encoded string and update the state after completing the read process. After that, you can render the image preview. For simplicity, I have not added any styling to the form element in the above example.

More great articles from LogRocket:

  • Don't miss a moment with The Replay, a curated newsletter from LogRocket
  • Learn how LogRocket's Galileo cuts through the noise to proactively resolve issues in your app
  • Use React's useEffect to optimize your application's performance
  • Switch between multiple versions of Node
  • Discover how to use the React children prop with TypeScript
  • Explore creating a custom mouse cursor with CSS
  • Advisory boards aren’t just for executives. Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

How to preview multiple images before upload in React

In this section, we shall look at how to preview multiple images before uploading in React with the FileReader API. Like the previous section, it assumes you have a React project set up already.

Reading and previewing multiple images is similar to previewing a single image. We shall modify the code in the previous section slightly. To browse and select several image files, you need to set the value of the multiple boolean attribute to true on the input element.

One noticeable difference is that we are looping through the FileList object in the useEffect Hook and reading the contents of all the selected files before updating the state. We are storing the data URL of each image file in an array, and updating state after reading the last file.

The code below is a modification of the previous example for previewing images in a batch:

import { useEffect, useState } from "react";const imageTypeRegex = /image\/(png|jpg|jpeg)/gm;function App() { const [imageFiles, setImageFiles] = useState([]); const [images, setImages] = useState([]); const changeHandler = (e) => { const { files } = e.target; const validImageFiles = []; for (let i = 0; i < files.length; i++) { const file = files[i]; if (file.type.match(imageTypeRegex)) { validImageFiles.push(file); } } if (validImageFiles.length) { setImageFiles(validImageFiles); return; } alert("Selected images are not of valid type!"); }; useEffect(() => { const images = [], fileReaders = []; let isCancel = false; if (imageFiles.length) { imageFiles.forEach((file) => { const fileReader = new FileReader(); fileReaders.push(fileReader); fileReader.onload = (e) => { const { result } = e.target; if (result) { images.push(result) } if (images.length === imageFiles.length && !isCancel) { setImages(images); } } fileReader.readAsDataURL(file); }) }; return () => { isCancel = true; fileReaders.forEach(fileReader => { if (fileReader.readyState === 1) { fileReader.abort() } }) } }, [imageFiles]); return ( <div className="App"> <form> <p> <label htmlFor="file">Upload images</label> <input type="file" id="file" onChange={changeHandler} accept="image/png, image/jpg, image/jpeg" multiple /> </p> </form> { images.length > 0 ? <div> { images.map((image, idx) => { return <p key={idx}> <img src={image} alt="" /> </p> }) } </div> : null } </div> );}export default App;

We keep references to the FileReader instances in an array for canceling any file reading process in the cleanup function when the component re-renders or unmounts to avoid memory leaks.

When using a routing library like React Router, a user can navigate away from the current page, and the component unmounts before completing the file reading process. Therefore, it is necessary to do cleanup as highlighted above.

In the above example, we are asynchronously reading the files in a loop and updating state after. Because of the asynchronous nature of the file reading process, it is impossible to know which file we shall complete reading last. Therefore, we have to check the number of files read in the load event handler before updating state. You can achieve the same with promises.

The code below shows a modification of the useEffect Hook to use promises instead. It is cleaner and easier to think about than using loops like in the previous method:

useEffect(() => { const fileReaders = []; let isCancel = false; if (imageFiles.length) { const promises = imageFiles.map(file => { return new Promise((resolve, reject) => { const fileReader = new FileReader(); fileReaders.push(fileReader); fileReader.onload = (e) => { const { result } = e.target; if (result) { resolve(result); } } fileReader.onabort = () => { reject(new Error("File reading aborted")); } fileReader.onerror = () => { reject(new Error("Failed to read file")); } fileReader.readAsDataURL(file); }) }); Promise .all(promises) .then(images => { if (!isCancel) { setImages(images); } }) .catch(reason => { console.log(reason); }); }; return () => { isCancel = true; fileReaders.forEach(fileReader => { if (fileReader.readyState === 1) { fileReader.abort() } }) }}, [imageFiles]);

Conclusion

Most web applications that require image upload from a client’s storage device also come with features for previewing images. Among other reasons, previewing an image ensures your clients upload image files of the appropriate type, quality, and size.

You can initiate file upload from a client’s device with an input element of type file or using the drag and drop interface. After selecting images, you can preview them using the URL API or the FileReader API. Though using the URL API may be straightforward, the FileReader API is not.

As highlighted in the article, you preview images singly or in a batch. Hopefully, this article gave you insights on image previews in React using the FileReader API. Let me know what you think in the comments section below.

Get set up with LogRocket's modern React error tracking in minutes:

  1. Visit https://logrocket.com/signup/ to getan app ID
  2. Install LogRocket via npm or script tag. LogRocket.init() must be called client-side, notserver-side

    • npm
    • Script tag
    $ npm i --save logrocket // Code:import LogRocket from 'logrocket'; LogRocket.init('app/id'); 
    // Add to your HTML:<script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script><script>window.LogRocket && window.LogRocket.init('app/id');</script> 
  3. (Optional) Install plugins for deeper integrations with your stack:
    • Redux middleware
    • NgRx middleware
    • Vuex plugin

Get started now

Get set up with LogRocket's modern error tracking in minutes:

  1. Visit https://logrocket.com/signup/ to getan app ID
  2. Install LogRocket via npm or script tag. LogRocket.init() must be called client-side, notserver-side

    • npm
    • Script tag
    $ npm i --save logrocket // Code:import LogRocket from 'logrocket'; LogRocket.init('app/id'); 
    // Add to your HTML:<script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script><script>window.LogRocket && window.LogRocket.init('app/id');</script> 
  3. (Optional) Install plugins for deeper integrations with your stack:
    • Redux middleware
    • NgRx middleware
    • Vuex plugin

Get started now

Using the FileReader API to preview images in React - LogRocket Blog (2024)

FAQs

How to preview an image in react? ›

To upload image and preview it using React JS we will use the HTML file input for the image input. After taking input the image url is created using URL. createObjectURL() method and store in the useState variable named file. Display the image as preview using the html img tags with the file url in the src prop.

How to display image from API in React JS? ›

import React, { useState, useEffect } from 'react'; import Image from './Image'; const App = () => { const [imageUrl, setImageUrl] = useState(null); const imageDescription = 'A dynamically fetched image'; useEffect(() => { fetch('https://api.example.com/random-image') . then(response => response.

What is FileReader in react? ›

The FileReader object 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 data to read.

How to upload file and preview in React JS? ›

Table of contents
  1. Setup a ReactJS app with Vite and TailwindCSS.
  2. Complete the uploader structure.
  3. Handling the file chooser.
  4. Customizing the uploader with reference(useRef)
  5. Handling the file change event.
  6. Previewing the uploaded image from the browser cache.
  7. Uploading the image to the server.
  8. A couple of tasks for you!
Mar 8, 2024

How do I use preview image? ›

Click the View menu. It's at the top of the File Explorer window. Select an option that displays previews. Choose any of these options to display image previews in the folder: Extra large icons, Large icons, Medium icons, Tiles, or Content.

How to make an image viewer in React? ›

Run the project

Open the command line console, then cd into the root of the React project. From there, run npm start . This will build the React application, and the ImageViewer will now be loaded into the browser. To load a new RasterImage into the ImageViewer, click Choose File and choose an image.

How to display images from an API? ›

How to Dynamically Display Images from an API Response in Angular
  1. Step 1: Creating the Image Service. We'll begin by setting up a service to handle the HTTP request for the image URL. ...
  2. Step 2: Fetching the Image URL in the Component. ...
  3. Step 3: Displaying the Image. ...
  4. Step 4: HttpClientModule Configuration.
Oct 24, 2023

How do I display API data in ReactJS? ›

Table of contents
  1. Create a React Application.
  2. Change your Project Directory.
  3. Access the API Endpoint.
  4. Import the useState() Hook and Set it to Hold Data.
  5. Create a FetchInfo() Callback Function to Fetch and Store Data.
  6. Output.

How do I display an image in React with URL? ›

Displaying an Image in ReactJS

In HTML, we use the <img> tag to display an image. The source of the image is defined with the src attribute. In React, we use the same <img> tag, and we can set the src attribute to a URL just like we would in HTML.

Why use FileReader? ›

FileReader is an object with the sole purpose of reading data from Blob (and hence File too) objects. It delivers the data using events, as reading from disk may take time.

How to read a file with FileReader? ›

The readAsText() method of the FileReader interface is used to read the contents of the specified Blob or File . When the read operation is complete, the readyState is changed to DONE , the loadend event is triggered, and the result property contains the contents of the file as a text string. Note: The Blob.

How do I get results from FileReader? ›

It works by creating a FileReader object and creating a listener for load events, such that when then file is read, the result is obtained and passed to the callback function provided to reader() . The content is handled as raw text data.

How do you display an image file in React? ›

To display a react image that is hosted on an external server in a React component, you can use the <img> HTML tag inside a component's render method. For example: This will render a react image with the specified src URL in your React component.

How do I display an image and PDF in react JS? ›

The simplest way to display a PDF document in React is by using the `<iframe>` element. You can embed the PDF file within an `<iframe>` tag and set the source to the URL of the PDF file.

How do I make an image appear in React? ›

Displaying an Image in ReactJS

In HTML, we use the <img> tag to display an image. The source of the image is defined with the src attribute. In React, we use the same <img> tag, and we can set the src attribute to a URL just like we would in HTML.

How do I read an image in React? ›

You can follow these steps to import local images to a React component and loop through them:
  1. Create an images folder and put your images inside this folder.
  2. Import each image file into your component file using import statements. You can define each imported image as a variable.

How do I preview a React component? ›

OR
  1. Open Visual Studio Code.
  2. From your working editor Press Ctrl + Shift + P / ⌘ + Shift + P to open command Palette and type "React Component Preview: preview start"
  3. Start editing your react component from the text editor and add props from the control panel.
Feb 18, 2022

How to preview an image in JavaScript? ›

Use the URL class and createObjectURL() method to generate a temporary URL representing the file. Set the image element's src attribute to the generated URL to display the image preview. To handle non-image files or cancellations, verify the file type using the "type" property.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6432

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.