File Upload

A drag-and-drop file upload component with progress tracking, file type icons, and upload status indicators. Perfect for document management and media uploads.

Drag & DropProgress BarFile TypesMulti-file

Installation

Terminal
ReactTailwind
TSX
npx @uiblox/cli add file-upload

Preview

Drop files or click to upload

Images, documents up to 50MB

design-specs.pdf

2.3 MB

Done

screenshot.png

825.2 KB

67%

Use Cases

Document Management

Upload and organize documents, PDFs, spreadsheets with automatic file type detection.

Media Libraries

Bulk upload images and videos with thumbnail previews and progress tracking.

Form Attachments

Allow users to attach supporting documents to forms and applications.

Cloud Storage

Build file storage interfaces with upload queues and batch processing.

Source Code

file-upload.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use client"; import { useState, useCallback } from "react"; import { cn } from "@/lib/utils"; interface UploadedFile { id: string; name: string; size: number; type: string; progress: number; status: "uploading" | "complete" | "error"; } export function FileUpload() { const [files, setFiles] = useState<UploadedFile[]>([]); const [isDragging, setIsDragging] = useState(false); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); }, []); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const droppedFiles = Array.from(e.dataTransfer.files); handleFiles(droppedFiles); }, []); const handleFileInput = (e: React.ChangeEvent<HTMLInputElement>) => { if (e.target.files) { handleFiles(Array.from(e.target.files)); } }; const handleFiles = (newFiles: File[]) => { const uploadFiles: UploadedFile[] = newFiles.map((file) => ({ id: Math.random().toString(36).substr(2, 9), name: file.name, size: file.size, type: file.type, progress: 0, status: "uploading" as const, })); setFiles((prev) => [...prev, ...uploadFiles]); // Simulate upload progress uploadFiles.forEach((file) => { simulateUpload(file.id); }); }; const simulateUpload = (fileId: string) => { let progress = 0; const interval = setInterval(() => { progress += Math.random() * 30; if (progress >= 100) { progress = 100; clearInterval(interval); setFiles((prev) => prev.map((f) => (f.id === fileId ? { ...f, progress: 100, status: "complete" } : f)) ); } else { setFiles((prev) => prev.map((f) => (f.id === fileId ? { ...f, progress } : f)) ); } }, 500); }; const removeFile = (fileId: string) => { setFiles((prev) => prev.filter((f) => f.id !== fileId)); }; const formatFileSize = (bytes: number) => { if (bytes === 0) return "0 Bytes"; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; }; return ( <div className="max-w-2xl mx-auto p-6"> {/* Drop Zone */} <div onDragOver={handleDragOver} onDragLeave={handleDragLeave} onDrop={handleDrop} className={cn( "relative border-2 border-dashed rounded-2xl p-12 text-center transition-all duration-300", isDragging ? "border-purple-500 bg-purple-50 dark:bg-purple-900/20" : "border-slate-300 dark:border-slate-700 hover:border-purple-400 dark:hover:border-purple-600" )} > <input type="file" multiple onChange={handleFileInput} className="absolute inset-0 w-full h-full opacity-0 cursor-pointer" /> <div className="space-y-4"> <div className="w-16 h-16 mx-auto bg-purple-100 dark:bg-purple-900/30 rounded-full flex items-center justify-center"> <UploadIcon className="w-8 h-8 text-purple-600" /> </div> <div> <p className="text-lg font-medium text-slate-900 dark:text-white"> Drop files here or click to upload </p> <p className="text-sm text-slate-500 mt-1"> Support for images, documents, and videos up to 50MB </p> </div> <button className="px-6 py-2.5 bg-purple-600 hover:bg-purple-700 text-white font-medium rounded-xl transition-colors"> Browse Files </button> </div> </div> {/* File List */} {files.length > 0 && ( <div className="mt-6 space-y-3"> <h3 className="text-sm font-semibold text-slate-900 dark:text-white"> Uploaded Files({files.length}) </h3> {files.map((file) => ( <div key={file.id} className="flex items-center gap-4 p-4 bg-white dark:bg-slate-900 rounded-xl border border-slate-200 dark:border-slate-800" > <div className="w-10 h-10 bg-slate-100 dark:bg-slate-800 rounded-lg flex items-center justify-center"> <FileIcon type={file.type} /> </div> <div className="flex-1 min-w-0"> <p className="text-sm font-medium text-slate-900 dark:text-white truncate"> {file.name} </p> <p className="text-xs text-slate-500">{formatFileSize(file.size)}</p> {file.status === "uploading" && ( <div className="mt-2 h-1.5 bg-slate-200 dark:bg-slate-700 rounded-full overflow-hidden"> <div className="h-full bg-purple-600 rounded-full transition-all duration-300" style={{ width: `${file.progress}%` }} /> </div> )} </div> <div className="flex items-center gap-2"> {file.status === "complete" && ( <span className="flex items-center gap-1 text-emerald-600 text-sm"> <CheckIcon /> Done </span> )} {file.status === "uploading" && ( <span className="text-sm text-slate-500">{Math.round(file.progress)}%</span> )} <button onClick={() => removeFile(file.id)} className="p-1.5 text-slate-400 hover:text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg transition-colors" > <XIcon /> </button> </div> </div> ))} </div> )} </div> ); } function UploadIcon({ className }: { className?: string }) { return <svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" /></svg>; } function FileIcon({ type }: { type: string }) { if (type.startsWith("image/")) { return <svg className="w-5 h-5 text-purple-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" /></svg>; } if (type.includes("pdf")) { return <svg className="w-5 h-5 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z" /></svg>; } return <svg className="w-5 h-5 text-slate-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" /></svg>; } function CheckIcon() { return <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" /></svg>; } function XIcon() { return <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /></svg>; }