Component setup
Start by setting up the React component with state variables for tracking upload progress and status:
isUploading
: Boolean flag to track upload state and disable UI elementsprogress
: Number to track upload completion percentage
File input validation
Implement file input handling with proper error checking:
- Get the file input element using
getElementById
- Extract the selected file from the input's
files
array - Validate that a file was actually selected before proceeding
- Show user-friendly error message if no file is selected
Upload configuration
Set up the core configuration parameters for the multipart upload:
- Base URL: Construct the API endpoint using
ASSETS_PREFIX
environment variable - File Key: Use the original filename as the storage key
- Chunk Size: Set to 5MB for optimal performance
- Total Parts: Calculate the number of chunks needed based on file size
Initiate upload
Begin the multipart upload by requesting an uploadId
from the server:
- Create the initiation URL with
action=create
parameter - Send POST request with file metadata (key and content type)
Store uploadId
The uploadId
is a unique identifier that links all parts of the multipart upload together. Store it securely as it will be used for all subsequent API calls.
Prepare parts for upload
Set up the data structures and URL templates needed for uploading individual parts:
- Parts Data Array: Initialize array to store part metadata (
PartNumber
andETag
) - Upload Part URL: Create URL template with required parameters:
action=upload-part
uploadId
key
for file identification
Upload file parts
Implement the core chunking and uploading logic:
- File Chunking: Use
file.slice()
to create 5MB chunks - Part Numbering: Assign sequential part numbers (1-based indexing)
- Upload: Upload each part sequentially
- Progress Tracking: Update progress bar after each successful part upload
Store part metadata
For each successfully uploaded part, store the identifying metadata in the partsData
array:
- Part Number: Sequential identifier for the part
- ETag: Server-generated hash for integrity verification
This metadata is required to complete the multipart upload successfully.
Complete multipart upload
Finalize the upload by combining all parts into a single file:
- Create completion URL with
action=complete
parameter - Send POST request with:
uploadId
: Links all parts togetherkey
: Final file nameparts
: Array of part metadata
Error handling and cleanup
Handle errors and reset the state:
- Try-Catch Block: Wrap entire upload process in error handling
- User Feedback: Show success/error messages to the user
- State Reset: Clean up upload state and progress in
finally
block
UI components
Create a clean, responsive UI for the upload functionality:
- File Input: Standard HTML file picker
- Upload Button: Disabled during upload with visual feedback
- Progress Bar: Real-time visual progress indicator
- Status Text: Percentage completion display
The UI provides immediate feedback and prevents multiple simultaneous uploads.