Uploader3
Uploader3 is a React-based Web3 image upload component that supports multiple image uploads, image cropping, and uploading images to Web3 Storage providers (like IPFS). There are two ways for uploading, by using a backend API or the Uploader3 Connector.
Features
- Supports cropping
- Supports uploading to web3 service providers, such as NFT.storage
Usage
import { Uploader3 } from '@lxdao/uploader3';
Props
Prop | Type | Description | Default |
---|---|---|---|
accept | string | image accept file type | ['.png','.jpeg','.jpg','.gif', '.svg'] |
multiple | boolean | multiple image upload | false |
api | string | endpoint upload api url, post method | |
headers | object | http headers to post api | |
responseFormat | function | response data format | |
connector | object | create by uploader3-connector | |
crop | Crop / boolean | crop config, set false disabled crop | true |
onChange | function | callback when files selected | |
onUpload | function | callback when file uploading | |
onComplete | function | callback when file uploaded | |
onCropEnd | function | callback when crop end | |
onCropCancel | function | callback when crop cancel |
api
api
upload serve must return the uploaded file url, and the url must be in the response body.
res.status(200).json({ url: 'https://example.com/xxx.png' });
If you have responseFormat
prop, the response body will be passed to responseFormat
function.
const responseFormat = (responseBody) => {
return {
url: responseBody.data.url,
};
};
api
and connector
are mutually exclusive, if both are provided, api
will be used. must be provided one of them.
Types reference
Crop
type Crop = {
size: { width: number; height: number };
aspectRatio: number;
};
Examples
Basic
Uploader3 children component has the ability to click and drag to select files.
Support svg file
Set accept
to ['.svg']
only pick svg files, and turn off cropping
Single file
Set crop
to true
to enable cropping. use Default crop config, Specify api
endpoint url to upload image.
const defaultCropOptions = {
size: { width: '500px', height: '400px' },
aspectRatio: 1,
};
If you choose a .svg
file and want to keep the file format and upload it directly, you can adjust the cropping ratio to full
.
Single file not crop
Set crop
to false
to disable cropping.
Frontend upload with connector
Set connector
to Uploader3Connector
instance to upload image to web3 storage provider.
Multiple files
Set multiple
to true
to enable multiple image upload. and enable cropping, when crop end, the cropped image will be one by one uploaded.
Multiple files not crop
Disable cropping. when select files, the files will be one by one uploaded.