AskHandle

AskHandle Blog

How to Handle File Uploads in React and Node.js

September 24, 2025Aria Singh3 min read

How to Handle File Uploads in React and Node.js

Have you ever wondered how to efficiently manage file uploads in your React and Node.js applications? This article will guide you through the process with clear examples and explanations.

File uploads are a common requirement in modern web applications, allowing users to share images, documents, and other types of files. Integrating file upload functionality in your React and Node.js projects can seem complex at first, but with the right approach, it can be streamlined and hassle-free.

Understanding the Basics

Before diving into the implementation details, it's essential to understand the basics of handling file uploads. In a typical setup, the client-side (React) is responsible for selecting files to upload, while the server-side (Node.js) receives and processes these files.

React provides a convenient way to capture file input from users using HTML <input> elements with the type="file" attribute. Once a user selects a file, React can send this file to the Node.js backend for processing.

Handling File Uploads in React

To handle file uploads in React, you can create a component that includes a file input field and a function to send the selected file to the server. Here's a simplified example:

jsx
1import React, { useState } from 'react';
2import axios from 'axios';
3
4const FileUpload = () => {
5  const [selectedFile, setSelectedFile] = useState(null);
6
7  const handleFileChange = (event) => {
8    setSelectedFile(event.target.files[0]);
9  };
10
11  const uploadFile = async () => {
12    const formData = new FormData();
13    formData.append('file', selectedFile);
14
15    await axios.post('/upload', formData, {
16      headers: {
17        'Content-Type': 'multipart/form-data',
18      },
19    });
20
21    // Handle the server response or perform additional actions
22  };
23
24  return (
25    <div>
26      <input type="file" onChange={handleFileChange} />
27      <button onClick={uploadFile}>Upload</button>
28    </div>
29  );
30};
31
32export default FileUpload;

In this example, the handleFileChange function updates the selectedFile state whenever a file is chosen. The uploadFile function sends the selected file to the server using Axios, a popular HTTP client for the browser and Node.js.

Handling File Uploads in Node.js

On the Node.js side, you need to set up a route to receive the uploaded file and save it to the server or perform any necessary processing. Here's a basic example using Express:

javascript
1const express = require('express');
2const multer = require('multer');
3const app = express();
4
5const storage = multer.diskStorage({
6  destination: function (req, file, cb) {
7    cb(null, 'uploads/'); // Save uploaded files to the 'uploads' directory
8  },
9  filename: function (req, file, cb) {
10    cb(null, file.originalname); // Maintain the original filename
11  },
12});
13
14const upload = multer({ storage });
15
16app.post('/upload', upload.single('file'), (req, res) => {
17  res.send('File uploaded successfully');
18});
19
20app.listen(5000, () => {
21  console.log('Server running on port 5000');
22});

In this Node.js example, we use the Multer middleware to handle file uploads. The uploaded file is saved to the specified directory with its original filename. The route /upload accepts a single file upload and sends a success message upon completion.

Handling File Upload Progress

It's advisable to provide feedback to users on the upload progress, especially for larger files. You can achieve this by tracking the upload progress on the client-side and displaying it accordingly. Here's an updated version of the React component with progress tracking:

jsx
1const FileUpload = () => {
2  const [selectedFile, setSelectedFile] = useState(null);
3  const [progress, setProgress] = useState(0);
4
5  const handleFileChange = (event) => {
6    setSelectedFile(event.target.files[0]);
7  };
8
9  const uploadFile = async () => {
10    const formData = new FormData();
11    formData.append('file', selectedFile);
12
13    await axios.post('/upload', formData, {
14      headers: {
15        'Content-Type': 'multipart/form-data',
16      },
17      onUploadProgress: (progressEvent) => {
18        const completed = Math.round((progressEvent.loaded * 100) / progressEvent.total);
19        setProgress(completed);
20      },
21    });
22
23    // Handle the server response or perform additional actions
24  };
25
26  return (
27    <div>
28      <input type="file" onChange={handleFileChange} />
29      <button onClick={uploadFile}>Upload</button>
30      {progress > 0 && <p>Upload Progress: {progress}%</p>}
31    </div>
32  );
33};

In this updated React component, the onUploadProgress event handler in the Axios request is used to track the upload progress. The setProgress function updates the progress state, which is then displayed to the user.

Handling file uploads in React and Node.js doesn't have to be a daunting task. With the right approach and understanding of the fundamentals, you can create a seamless file upload experience for your users. By following the examples and guidelines provided in this article, you'll be well-equipped to integrate file upload functionality into your projects effectively.

Are you ready to enhance your applications with robust file upload capabilities? Give it a try and empower your users to share and exchange files effortlessly!