sureshdeveloperofficial's picture
Upload folder using huggingface_hub
9aefa19 verified
Raw History Blame Contribute Delete
3.51 kB
import { useCallback } from 'react'
function FileUploader({ onFileSelect, file, fileType }) {
const handleDrop = useCallback((e) => {
e.preventDefault()
const droppedFile = e.dataTransfer.files[0]
if (droppedFile) {
onFileSelect(droppedFile)
}
}, [onFileSelect])
const handleDragOver = useCallback((e) => {
e.preventDefault()
}, [])
const handleFileInput = useCallback((e) => {
const selectedFile = e.target.files[0]
if (selectedFile) {
onFileSelect(selectedFile)
}
}, [onFileSelect])
return (
<div>
<h2 className="text-2xl font-semibold mb-4 text-gray-800">Upload File</h2>
<div
onDrop={handleDrop}
onDragOver={handleDragOver}
className="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center hover:border-indigo-500 transition-colors cursor-pointer"
>
<input
type="file"
id="file-upload"
className="hidden"
accept={fileType === 'image' ? 'image/*' : fileType === 'video' ? 'video/*' : 'image/*,video/*'}
onChange={handleFileInput}
/>
<label htmlFor="file-upload" className="cursor-pointer">
<div className="space-y-4">
<svg
className="mx-auto h-12 w-12 text-gray-400"
stroke="currentColor"
fill="none"
viewBox="0 0 48 48"
>
<path
d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
<div>
<p className="text-lg text-gray-700">
Drag and drop your file here, or{' '}
<span className="text-indigo-600 font-semibold">click to browse</span>
</p>
<p className="text-sm text-gray-500 mt-2">
Supports images (PNG, JPG, etc.) and videos (MP4, etc.)
</p>
</div>
</div>
</label>
</div>
{file && (
<div className="mt-4 p-4 bg-gray-50 rounded-lg">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-3">
<svg className="h-8 w-8 text-indigo-600" 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>
<div>
<p className="font-medium text-gray-800">{file.name}</p>
<p className="text-sm text-gray-500">
{(file.size / 1024 / 1024).toFixed(2)} MB • {fileType}
</p>
</div>
</div>
<button
onClick={() => onFileSelect(null)}
className="text-red-600 hover:text-red-800"
>
<svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
)}
</div>
)
}
export default FileUploader