@bytesbank/client
v0.0.2
Published
The Bytes Bank Client SDK is a comprehensive JavaScript library designed to simplify file uploads to your media storage solutions. It provides developers with an easy-to-use interface to manage media uploads with built-in features such as progress trackin
Downloads
140
Maintainers
Readme
Bytes Bank Client SDK
Introduction
The Bytes Bank Client SDK is a comprehensive JavaScript library designed to simplify file uploads to your media storage solutions. It provides developers with an easy-to-use interface to manage media uploads with built-in features such as progress tracking, error handling, and the ability to abort uploads.
Features
- Easy file uploads
- Progress tracking
- Upload success and error handling
- Ability to abort ongoing uploads
- Parse Image/Video file to base64
Installation
To install the SDK, run the following command in your terminal:
npm install bytes-bank-client
Usage
Step 1: Import the SDK Client
First, import the SDK into your JavaScript or TypeScript file:
import { BytesBankClient } from "bytes-bank-client";
Step 2: Create an Instance of the SDK
To ensure stability and prevent data loss during re-renders, instantiate the SDK outside the component boundary. You need to provide the collection name, and you can optionally include the clientKey
and clientSecret
:
const client = new BytesBankClient({
collection: "your collection name",
clientKey: "your_client_key", // Optional if set in environment variables
clientSecret: "your_client_secret", // Optional if set in environment variables
});
Step 3: Upload a File
To upload a file, use the send method. This method requires three parameters:
- Your selected file: The actual media file you want to upload.
- File identification: A unique identifier for the file, which can be a random number or string.
- Upload callback: A callback function that handles various upload events.
The upload callback receives two parameters: action and event.
The action can represent different states of the upload process:
- progress: This action occurs while the file is uploading, providing the current upload progress as a percentage.
- success: This action occurs when the file has been successfully uploaded, returning the response for the uploaded media.
- abort: This action occurs when you abort the upload process, returning the file ID.
- error: This action occurs if there is an error during the upload, providing the reason for the failure.
The events contains two properties: fileId and data
- file id is basic file identification
- data contains the success or error response
Here’s an example of how to implement the upload functionality:
const uploadingCallbacks = (action: string, event: any) => {
const { fileId, data } = event;
switch (action) {
case "progress":
console.log(`Uploading Progress: ${data}%`);
break;
case "success":
console.log("Upload Success.", event);
break;
case "abort":
console.log("Upload aborted.");
break;
case "error":
console.log("Uploading Error.", event);
break;
default:
break;
}
};
client.send("your media file", "unique file ID", uploadingCallbacks);
Step 4: Abort an Upload
If you need to abort an ongoing upload, you can use the abort
method with the file ID:
client.abort("file id");
Additional Feature: Media Parsing
The Bytes Bank Client SDK includes a media parsing feature that allows you to convert media files (both images and videos) into their base64 representations. This is particularly useful for scenarios where you need to preview media files before uploading them or when you want to embed media directly into HTML or JSON data.
Valid File Types
The mediaParser function only processes files that are either images or videos. If a file type is invalid, its corresponding entry in the parsed media array will have a null base64 value, allowing you to handle it gracefully in your application.
This media parsing capability enhances the functionality of the Bytes Bank Client SDK, making it easier for developers to work with media files before uploading them.
How to Use the Media Parsing Feature
You can utilize the mediaParser
function from the SDK to process either a FileList
(which represents multiple files selected through an input element) or a single File
. The function will return an array of parsed media objects, each containing the original file, a unique identifier, and its base64 representation.
Example Usage
Here's a step-by-step guide on how to use the mediaParser
function:
Import the mediaParser Function
First, ensure you import the
mediaParser
function from the SDK:import { mediaParser } from "bytes-bank-client";
Call the mediaParser function
The function will return an array of parsed media objects, each containing the original file, a unique identifier, and its base64 representation.
const files = document.getElementById("fileInput").files; // Assuming a file input element or your uploaded files const parsedMedia = await mediaParser(files); console.log(parsedMedia);
Conclusion
The Bytes Bank Client SDK is designed to make file uploads simple and efficient. By following these steps, you can easily integrate media upload functionality into your application, as well as leverage the additional media parsing feature. For further details, refer to the LinkedIn