Axios is a powerful JavaScript library for making HTTP requests, including file uploads.
Asynchronous tasks, such as sending HTTP requests, are essential in web development to prevent blocking other processes from happening.
In this guide, we'll learn how to upload files in React using Axios.
Before we can start uploading files in a React app using Axios, we need to set it up first. For this, we'll use create-react-app
, a popular tool for creating React applications.
To create a new React project using create-react-app
, follow these steps:
sh
npx create-react-app my-app
This will create a new React project called my-app
in your current directory. Now navigate to the my-app
directory.
Next, we need to install Axios. To do this, run the following command in your terminal:
sh
npm install axios
Now that we've set up our React project and installed axios, it's time to create the file upload component.
To create the file upload component, follow these steps:
src
folder.components
.FileUpload.js
.FileUpload
that returns a div element:js
import React from 'react';const FileUpload = () => {return (<div>{/* File upload form goes here */}</div>);};export default FileUpload;
Next, we'll add the necessary event listeners to capture file inputs. Let’s use the File Upload API from the Rapid API hub for this particular example.
Please go ahead and subscribe to the File Upload API so you can easily call it in your React app.
js
import React from "react";import axios from "axios";const FileUpload = () => {const handleFileUpload = (event) => {// get the selected file from the inputconst file = event.target.files[0];// create a new FormData object and append the file to itconst formData = new FormData();formData.append("file", file);// make a POST request to the File Upload API with the FormData object and Rapid API headersaxios.post("https://file-upload8.p.rapidapi.com/upload", formData, {headers: {"Content-Type": "multipart/form-data","x-rapidapi-host": "file-upload8.p.rapidapi.com","x-rapidapi-key": "your-rapidapi-key-here",},}).then((response) => {// handle the responseconsole.log(response);}).catch((error) => {// handle errorsconsole.log(error);});};// render a simple input element with an onChange event listener that calls the handleFileUpload functionreturn (<div><input type="file" onChange={handleFileUpload} /></div>);};export default FileUpload;
The handleFileUpload
function is triggered when the user selects a file using the file input field. It extracts the first file from the event target and creates a new FormData
object to store it. Then, it uses Axios to send a POST
request to the File Upload API endpoint with the formData
as the data payload.
Head over to your App.js
file and paste this code there:
js
import React from 'react';import FileUpload from './components/FileUpload';function App() {return (<div className="App"><FileUpload /></div>);}export default App;
By using the Axios library to make HTTP requests, we can easily send file data to an API endpoint for processing. With the example implementation using the File Upload API, we were able to see how this process can be accomplished in a practical way.
We have also written a guide on how to send files to server with Fetch API, which explains how we can achieve something similar but with browser native fetch API.