Google Advertisement

How to Integrate Cloudinary Image Upload & Preview in React Step-by-Step

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

Learn how to upload and preview images in a React app using Cloudinary. A complete step-by-step guide with code examples, explanations, and best practices

Published on 24 May 2025
By Priya

Image uploads are a common feature in modern web apps, whether you're building a social media platform, profile dashboard, or product gallery. In this tutorial, you'll learn how to upload and preview images using React and Cloudinary, a cloud-based media management platform.


βœ… What Will You Learn?


🌐 Step 1: Create a Cloudinary Account

If you don’t already have one, go to https://cloudinary.com and sign up.

Once signed up:

We'll use only the Cloud name for client-side uploads using unsigned presets.


πŸ” Step 2: Set Up an Unsigned Upload Preset

  1. Go to Settings > Upload tab.

  2. Scroll down to Upload presets

  3. Click on Add upload preset

  4. Enable Unsigned

  5. Name it something like react_unsigned

  6. Click Save


βš™οΈ Step 3: Create a New React App

You can use create-react-app to bootstrap your project.

npx create-react-app react-cloudinary-upload
cd react-cloudinary-upload
npm start

Step 4: Install Axios

We'll use axios to make HTTP requests to Cloudinary.

npm install axios

Step 5: Create the Upload Component

Create a new file: UploadImage.js

import React, { useState } from 'react';
import axios from 'axios';

const UploadImage = () => {
  const [selectedImage, setSelectedImage] = useState(null);
  const [previewUrl, setPreviewUrl] = useState('');
  const [uploadedImageUrl, setUploadedImageUrl] = useState('');

  // Preview the selected image
  const handleImageChange = (e) => {
    const file = e.target.files[0];
    setSelectedImage(file);
    setPreviewUrl(URL.createObjectURL(file));
  };

  // Upload to Cloudinary
  const handleUpload = async () => {
    if (!selectedImage) return;

    const formData = new FormData();
    formData.append('file', selectedImage);
    formData.append('upload_preset', 'react_unsigned'); // Your unsigned preset

    try {
      const res = await axios.post(
        'https://api.cloudinary.com/v1_1/<your_cloud_name>/image/upload',
        formData
      );
      setUploadedImageUrl(res.data.secure_url);
    } catch (err) {
      console.error('Upload error:', err);
    }
  };

  return (
    <div style={{ textAlign: 'center' }}>
      <h2>Upload and Preview Image</h2>
      <input type="file" onChange={handleImageChange} accept="image/*" />
      <br /><br />
      {previewUrl && (
        <div>
          <h4>Preview:</h4>
          <img src={previewUrl} alt="Preview" width="300" />
        </div>
      )}
      <br />
      <button onClick={handleUpload}>Upload to Cloudinary</button>

      {uploadedImageUrl && (
        <div>
          <h4>Uploaded Image:</h4>
          <img src={uploadedImageUrl} alt="Uploaded" width="300" />
        </div>
      )}
    </div>
  );
};

export default UploadImage;

Explanation of Code

βœ… useState

We use it to manage:

βœ… handleImageChange

Fired when the user selects an image. It sets both:

βœ… handleUpload

This function sends the image to Cloudinary:


πŸ–ΌοΈ Step 6: Use the Component in App.js

Update your App.js file:

import React from 'react';
import UploadImage from './UploadImage';

function App() {
  return (
    <div className="App">
      <UploadImage />
    </div>
  );
}

export default App;

Output

When you run the app:


πŸ›‘οΈ Best Practices


πŸ”š Conclusion

With just a few steps, you’ve now integrated image upload and preview in a React app using Cloudinary. This is great for user profiles, product images, blog thumbnails, and much more.

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website