File Upload Example
Demonstrates the FileUploader component. The upload URL is configured via the NEXT_PUBLIC_UPLOAD_URL environment variable.
Current configuration
NEXT_PUBLIC_UPLOAD_URL
https://api.sparrowzoo.com/upload.jsonpathType
imFileUploader Usage Guide
Overview
FileUploader is a reusable upload component that wraps the full flow: select file → upload with token → progress → callback.
- Location:
src/common/components/file/FileUploader.tsx - Stack: React (client) + axios + react-hot-toast + next-intl
Quick Start
import FileUploader from "@/common/components/file/FileUploader";
import {UPLOAD_URL} from "@/common/lib/Env";
<FileUploader
url={UPLOAD_URL}
uploadCallback={(url, fileName) => console.log(url, fileName)}
uploadIcon={<button>Upload</button>}
id="my-uploader"
/>
Environment Variables
The upload URL is configured via NEXT_PUBLIC_UPLOAD_URL, read from Env.ts.
| Environment | Value |
|---|---|
| development | http://localhost:8888/upload.json |
| production | https://api.sparrowzoo.com/upload.json |
The
NEXT_PUBLIC_prefix is required; Next.js only inlines prefixed vars to the client.
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| url | string | Yes | - | Upload endpoint |
| uploadCallback | (url, fileName) => void | Yes | - | Success callback |
| uploadIcon | ReactNode | Yes | - | Trigger icon |
| id | string | Yes | - | Unique input id |
| pathType | string | No | "im" | Upload path type |
Upload Flow
- Click uploadIcon to open the file picker
- Validate file (empty → toast "select-file")
- Build FormData (file + pathType)
- Get token from CrosStorage
- axios POST with live progress
- Check result.code (non-"0" → error toast)
- On success, invoke uploadCallback
- Reset uploading & progress
Result Type
interface Result {
code: string; // "0" means success
message?: string; // error message
data: any; // business data (file url etc.)
}
i18n
| Key | zh | en |
|---|---|---|
| select-file | 请选择文件 | Please select a file |
| upload-success | 上传成功 | Upload successful |
| upload-failed | 上传失败: {message} | Upload failed: {message} |