Skip to main content

Upload

Upload file by selecting or dragging.

When To Use

Uploading is the process of publishing information (web pages, text, pictures, video, etc.) to a remote server via a web page or upload tool.

  • When you need to upload one or more files
  • When you need to show the process of uploading
  • When you need to upload files by dragging and dropping

Examples

Basic Usage

import { TxUpload } from "tx-design-ui";
import { useState } from "react";

function App() {
const [fileList, setFileList] = useState([]);

return <TxUpload fileList={fileList} onChange={setFileList} />;
}

Default File List

const defaultFiles = [
{
uid: "1",
name: "document.pdf",
status: "done",
url: "https://example.com/document.pdf",
},
{
uid: "2",
name: "image.png",
status: "done",
url: "https://example.com/image.png",
},
];

<TxUpload defaultFileList={defaultFiles} />;

Multiple Files

const [fileList, setFileList] = useState([]);

<TxUpload multiple fileList={fileList} onChange={setFileList} />;

Picture Card

Use listType="picture-card" for image upload preview.

const [fileList, setFileList] = useState([]);

<TxUpload listType="picture-card" accept="image/*" multiple fileList={fileList} onChange={setFileList} />;

Disabled

<TxUpload disabled />

Custom Accept

Limit file types using the accept prop.

<TxUpload accept="image/*" multiple />
<TxUpload accept=".pdf,.doc,.docx" />

Max File Size

Set maximum file size in MB.

<TxUpload maxSize={2} /> {/* Max 2MB */}

Custom Upload Area

<TxUpload>
<div
style={{
padding: "20px",
border: "2px dashed #00a4db",
borderRadius: "8px",
textAlign: "center",
cursor: "pointer",
}}
>
<div>📁</div>
<div>Drop files here or click to browse</div>
</div>
</TxUpload>

Drag and Drop

Drag and drop is supported by default. Simply drag files over the upload area.

API

PropertyDescriptionTypeDefault
acceptFile types that can be acceptedstring'*'
multipleAllow multiple file uploadbooleanfalse
disabledDisable uploadbooleanfalse
maxSizeMax file size in MBnumber-
fileListList of files (controlled)array[]
defaultFileListDefault list of filesarray[]
listTypeBuilt-in stylesheets'text' | 'picture' | 'picture-card''text'
showUploadListShow file listbooleantrue
childrenCustom upload area contentReactNode-
onChangeCallback when file list changesfunction(fileList)-
onRemoveCallback when file is removedfunction(file)-
beforeUploadHook before uploading, return false to cancelfunction(file)-
customRequestOverride default upload behaviorfunction(options)-
classNameAdditional CSS classstring-

customRequest Options

The customRequest function receives an options object with the following properties:

{
file: File; // The file to upload
onProgress: (percent: number) => void; // Update progress
onSuccess: () => void; // Called when upload succeeds
onError: () => void; // Called when upload fails
}

File Object

{
uid: string; // Unique file id
name: string; // File name
status: string; // 'uploading' | 'done' | 'error'
percent: number; // Upload progress (0-100)
originFileObj: File; // Original file object
url: string; // File URL
}