In this step-by-step guide, we will show you how to create a drag and drop file or image uploading and image preview component in React using FilePond, FilePondPluginImageExifOrientation, and FilePondPluginImagePreview libraries.
FilePond is a handy JavaScript library that makes file uploading utterly effortless.
For React environment, FilePond offers a handy wrapper component. It is basically a JavaScript library that uploads everything that comes across its path.
Not just that, it gives the great silky smooth user experience by promising image optimization for faster uploads in React.
In this quick tutorial, we will start with creating a new app, configure file pond plugin, make a specific drag and drop image uploading component and eventually show you the progress on the browser.
Not yet created the project, don’t worry here is how you can create your first react project.
On your terminal’s console, add the following command and hit enter. BTW, we assume you have already installed node and npm tools.
npx create-react-app react-proxima
A new blank react app is ready to be developed, just cd into the project.
cd react-proxima
In order to work with FilePond, it needs to be added to React project. We will altogether install filepond and react-filepond packages.
npm install filepond react-filepond
To create the image preview feature while file is uploading, also install the following packages.
npm install filepond-plugin-image-exif-orientation filepond-plugin-image-preview
To enable the image preview for file pond, we have to import the Image EXIF Orientation and Image Preview plugins altogether.
npm i filepond-plugin-image-preview filepond-plugin-image-exif-orientation
Let us create the components/ directory in your project’s root folder, you also need to create a FilePondUpload.js file.
To set up the functional component you have to add the following code in the file.
import React from 'react'
export default function FilePondUpload() {
return (
<div>
</div>
)
}
First, import the React FilePond and registerPlugin modules. File pond comes with its own styling; to enable that, you have to import the CSS of respective modules.
The registerPlugin() method is used to invoke the file pond image preview CSS, also install and import it.
Set the useState lifecycle hook to initialize the files state. Define the FilePond directive and pass the given properties into it; you can pass your own API or server URL where you store the uploaded image.
import React, { useState } from "react";
// Import React FilePond
import { FilePond, registerPlugin } from "react-filepond";
// Import FilePond styles
import "filepond/dist/filepond.min.css";
import FilePondPluginImageExifOrientation from "filepond-plugin-image-exif-orientation";
import FilePondPluginImagePreview from "filepond-plugin-image-preview";
import "filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css";
// Register the plugins
registerPlugin(FilePondPluginImageExifOrientation, FilePondPluginImagePreview);
function FilePondUpload() {
const [files, setFiles] = useState([]);
return (
<div className="container mt-4">
<h2 className="mb-3">React FilePond File Upload Example</h2>
<FilePond
allowMultiple={true}
files={files}
maxFiles={5}
allowReorder={true}
server="" // File upload api goes here
/>
</div>
)
}
export default FilePondUpload
Since the App component is the significant component in React application, it works as a container for all other components.
You have to open the src/App.js file and import the FilePondUpload component here.
import React from 'react'
import FilePondUpload from './components/FilePondUpload.js'
import 'bootstrap/dist/css/bootstrap.min.css'
function App() {
return (
<div>
<FilePondUpload />
</div>
)
}
export default App;
Now, you must be waiting to see the results on the browser. In order to view the app, you first run the development server.
npm start
Copy the given url and add it on the browser’s address bar and hit enter.
http://localhost:3000
We have successfully built the image uploading component in React with file drag and drop support. However, this is just the beginning. It comprises various other features that you can use to enhance the feature of React file uploading functionality.
Here are the core features of FilePond:
For making the additional features work, make sure to add some other dependencies with FilePond.
In this post, we will learn how to work with HTTP requests in the Redux…
MySQL is a relational database management system based on SQL – Structured Query Language, and…
React Js Handle Rest API data globally with Context and useState hook tutorial. In this…
Node js delete data from MySQL database tutorial; Throughout this guide, you will ascertain how…
In this tutorial, you will discover how to import CSV file into MySQL database using…
React Js Global state management with createContext hook example; In this tutorial, we will help…