Set up resumable uploads for Web
The FastPix Resumable Uploads SDK helps you efficiently upload large files from the browser by splitting them into chunks and also gives you the ability to pause and resume your uploads.
How resumable uploads work through chunking
Resumable uploads can be effectively handled through a technique called chunking. This method involves breaking down large files into smaller, more manageable pieces, or “chunks.” Here’s how chunking works:
- Divide the file: Large files are split into smaller, manageable chunks (e.g., 16 MB by default).
- Upload individually: Each chunk is uploaded separately. If a chunk fails, only that specific chunk needs to be re-uploaded.
- Resume capability: If the upload is interrupted, you can resume from the last successfully uploaded chunk instead of starting over.
This approach is important because:
- It reduces the risk: Uploading smaller chunks minimizes the risk of failure. If a chunk fails to upload due to network issues, only that specific chunk needs to be re-uploaded, not the entire file.
- Improves performance: Smaller chunks can be uploaded more quickly and efficiently, especially on slower connections, as they require less time to transfer.
- Easier management: Chunking allows for better tracking of upload progress, making it easier to implement features like pause and resume.
Step 1: Installation
To install the SDK, you can use NPM, a CDN, or your preferred package manager:
Using NPM:
Using CDN:
Basic usage
To get started with the SDK, you will need a signed URL.
To make API requests, you’ll need a valid Access Token and Secret Key. See the Basic Authentication Guide for details on retrieving these credentials.
Once you have your credentials, use the Upload media from device API to generate a signed URL for uploading media. After fetching the the signed URL you can continue with integrating the SDK into your application.
The example below gives you an idea about how to get the signed URL using the upload media from device API. You can use the example directly or also refer to our upload videos directly guide.
PLEASE NOTE
When making Basic Auth API calls, securely retrieve the username and password from environment variables (.env) or a protected server endpoint to prevent unauthorized access.
In the API response, you will get a signed URL upon successful API request. Next step is to take the signed URL and pass it to the SDK.
Step 2: Import the SDK
Step 3: Integration
Parameters to use
This SDK supports the following parameters:
endpoint(mandatory): The URL endpoint where the file will be uploaded.file(mandatory): The file object that you want to upload (e.g., a video file).chunkSize(optional): chunkSize should be provided in kilobytes (KB). By default, the SDK splits files into chunks of 16 MB. You can customize this by specifying a chunkSize in kilobytes (minimum of 5 MB).maxFileSize(optional): This parameter restricts the maximum file size that users can upload. Setting this can help prevent users from attempting to upload excessively large files that may not be manageable.retryChunkAttempt(optional): Specifies the number of times to retry uploading a chunk in case of failure. The default value is 5.delayRetry(optional): The delay time (in milliseconds) before retrying a chunk upload after a failure. The default value is 1000ms.
Monitor the upload progress through lifecycle events
Using the following lifecycle events lets you to build a robust and user-friendly upload experience. By tracking progress in real-time, handling errors proactively, managing chunk retries, and responding to network changes, you can ensure seamless and efficient uploads.
Integrating these event handlers into your application can enhance reliability, optimize performance, and provide users with clear, actionable feedback throughout the upload process. See detailed usage example .
Managing video uploads
You can control the upload lifecycle with the following methods:
Pause an upload:
Resume an upload:
Abort an upload:
Parameters Accepted
This SDK supports the following parameters:
- endpoint (mandatory): The URL endpoint where the file will be uploaded.
- file (mandatory): The file object that you want to upload (e.g., a video file).
- chunkSize (optional): chunkSize should be provided in kilobytes (KB). By default, the SDK splits files into chunks of 16 MB. You can customize this by specifying a chunkSize in kilobytes (minimum of 5 MB).
- maxFileSize (optional): This parameter restricts the maximum file size that users can upload. Setting this can help prevent users from attempting to upload excessively large files that may not be manageable.
- retryChunkAttempt (optional): Specifies the number of times to retry uploading a chunk in case of failure. The default value is 5.
- delayRetry (optional): The delay time (in milliseconds) before retrying a chunk upload after a failure. The default value is 1000ms.
Usage example of resumable uploads SDK
The following examples give an overview of integrating the FastPix Resumable Uploads SDK into your project, enabling you to build a fully customized upload interface. By making use of the SDK’s lifecycle events and configurable attributes, you can enhance functionality and optimize the upload experience.
Changelog
All notable changes to the Resumable uploads SDK for web will be documented below.
Current version
v[1.0.0]
Features:
- Chunking: Files are automatically split into chunks (default chunk size is 16MB).
- Pause and Resume: Allows temporarily pausing the upload and resuming after a while.
- Retry: Uploads might fail due to temporary network failures. Individual chunks are retried for 5 times with exponential backoff to recover automatically from such failures.
- Lifecycle Event Listeners: Provides real-time feedback through various upload lifecycle events.
- Error Handling: Comprehensive error management to notify users of issues during uploads.
- Customizability: Options to customize chunk size and retry attempts.