React Upload Component

4.11.0 · active · verified Sun Apr 19

rc-upload is a lightweight, unstyled React UI component for handling file uploads. It provides foundational upload capabilities such as drag-and-drop, folder uploads, and paste-from-clipboard functionality without imposing any specific visual design, allowing developers full control over the user interface. The current stable version is 4.11.0. The project maintains an active release cadence, with minor versions released periodically to introduce new features, address bugs, and update dependencies. Its key differentiators include its headless architecture, extensive customization options via props like `customRequest` and `beforeUpload`, and support for modern browser-based upload features. It is a core utility often used as a building block for more complex upload components.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic file upload component with a mock server action and callback handlers for `onStart`, `onSuccess`, `onError`, `onProgress`, and `beforeUpload` to manage file state and validation.

import React, { useState } from 'react';
import Upload from 'rc-upload';
import type { UploadProps, RcFile } from 'rc-upload';

const App: React.FC = () => {
  const [fileList, setFileList] = useState<RcFile[]>([]);

  const handleStart: UploadProps['onStart'] = (file) => {
    console.log('onStart', file.name);
    setFileList((prev) => [...prev, { ...file, status: 'uploading' }]);
  };

  const handleSuccess: UploadProps['onSuccess'] = (response, file) => {
    console.log('onSuccess', response, file.name);
    setFileList((prev) => prev.map(f => f.uid === file.uid ? { ...f, status: 'done' } : f));
  };

  const handleError: UploadProps['onError'] = (err, response, file) => {
    console.error('onError', err, response, file.name);
    setFileList((prev) => prev.map(f => f.uid === file.uid ? { ...f, status: 'error' } : f));
  };

  const handleProgress: UploadProps['onProgress'] = (event, file) => {
    console.log('onProgress', Math.round(event.percent || 0) + '%', file.name);
  };

  const beforeUpload: UploadProps['beforeUpload'] = (file, currentFileList) => {
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
      alert('File must be smaller than 2MB!');
      return false; // Stop upload
    }
    console.log('beforeUpload', file.name, currentFileList.length);
    return true; // Proceed with upload
  };

  return (
    <div>
      <h1>File Upload Demo</h1>
      <Upload
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76" // Mock API endpoint
        method="POST"
        name="myFile"
        multiple={true}
        onStart={handleStart}
        onSuccess={handleSuccess}
        onError={handleError}
        onProgress={handleProgress}
        beforeUpload={beforeUpload}
        withCredentials={false} // Adjust as needed for your backend
        style={{
          padding: '20px',
          border: '2px dashed #ccc',
          textAlign: 'center',
          cursor: 'pointer',
          margin: '20px 0'
        }}
      >
        <button type="button">Click to Upload or Drag File Here</button>
      </Upload>
      {fileList.length > 0 && (
        <div>
          <h2>Upload Status:</h2>
          <ul>
            {fileList.map((file) => (
              <li key={file.uid}>
                {file.name} - {file.status} {file.status === 'uploading' && '(in progress)'}
              </li>
            ))}
          </ul>
        </div>
      )}
    </div>
  );
};

export default App;

view raw JSON →