मुख्य सामग्री पर जाएं

रिटर्न वैल्यूज संदर्भ

useDropup हुक द्वारा लौटाई गई सभी चीजें।

अवलोकन

const {
files, // फाइल ऑब्जेक्ट्स की सरणी
state, // वर्तमान स्थिति
actions, // उपलब्ध क्रियाएं
getDropProps, // ड्रॉप जोन के लिए Props
getInputProps, // इनपुट एलिमेंट के लिए Props
openFileDialog, // फाइल पिकर खोलें
} = useDropup();

files

सभी जोड़ी गई फाइलों को दर्शाने वाले DropupFile ऑब्जेक्ट्स की सरणी।

const { files } = useDropup();

files.forEach(file => {
console.log(file.id); // "abc123"
console.log(file.name); // "photo.jpg"
console.log(file.size); // 1234567
console.log(file.type); // "image/jpeg"
console.log(file.status); // "idle" | "uploading" | "complete" | "error"
console.log(file.progress); // 0-100
console.log(file.preview); // "blob:http://..." (केवल छवियां)
console.log(file.uploadedUrl); // "https://..." (अपलोड के बाद)
console.log(file.error); // DropupError (अगर विफल)
});

DropupFile गुण

गुणप्रकारविवरण
idstringअद्वितीय पहचानकर्ता
fileFileमूल ब्राउज़र File ऑब्जेक्ट
namestringफाइल का नाम
sizenumberबाइट्स में आकार
typestringMIME प्रकार
statusFileStatusवर्तमान स्थिति
progressnumberअपलोड प्रगति (0-100)
previewstring | undefinedछवियों के लिए प्रीव्यू URL
uploadedUrlstring | undefinedसफल अपलोड के बाद URL
responseunknownसर्वर प्रतिक्रिया डेटा
errorDropupError | undefinedअपलोड विफल होने पर Error
metaRecord<string, unknown>कस्टम मेटाडेटा

FileStatus मान

स्थितिविवरण
'idle'फाइल जोड़ी गई, अपलोड नहीं हो रही
'uploading'अपलोड प्रगति में
'complete'अपलोड सफल
'error'अपलोड विफल
'paused'अपलोड रुका हुआ (पुनः शुरू करने योग्य अपलोड)

state

ड्रॉपजोन की वर्तमान स्थिति।

const { state } = useDropup();

console.log(state.isDragging); // ड्रैग करते समय true
console.log(state.isDragAccept); // ड्रैग की गई फाइलें वैध होने पर true
console.log(state.isDragReject); // ड्रैग की गई फाइलें अवैध होने पर true
console.log(state.isUploading); // कोई फाइल अपलोड होने पर true
console.log(state.progress); // समग्र प्रगति 0-100
console.log(state.status); // 'idle' | 'uploading' | 'complete' | 'error'

स्टेट गुण

गुणप्रकारविवरण
isDraggingbooleanड्रॉप जोन पर फाइलें ड्रैग की जा रही हैं
isDragAcceptbooleanड्रैग की गई फाइलें सत्यापन पास करती हैं
isDragRejectbooleanड्रैग की गई फाइलें सत्यापन में विफल
isUploadingbooleanकोई फाइल वर्तमान में अपलोड हो रही है
progressnumberसभी फाइलों की औसत प्रगति (0-100)
statusDropupStatusसमग्र अपलोड स्थिति

DropupStatus मान

स्थितिविवरण
'idle'कोई अपलोड प्रगति में नहीं
'uploading'एक या अधिक फाइलें अपलोड हो रही हैं
'complete'सभी अपलोड सफलतापूर्वक समाप्त
'error'एक या अधिक अपलोड विफल

actions

सभी उपलब्ध क्रियाओं वाला ऑब्जेक्ट।

const { actions } = useDropup();

// अपलोड शुरू करें
actions.upload(); // सभी idle फाइलें अपलोड करें
actions.upload(['id1', 'id2']); // विशिष्ट फाइलें अपलोड करें

// अपलोड रद्द करें
actions.cancel(); // सभी अपलोड रद्द करें
actions.cancel('file-id'); // विशिष्ट फाइल रद्द करें

// फाइलें हटाएं
actions.remove('file-id'); // विशिष्ट फाइल हटाएं
actions.reset(); // सभी फाइलें हटाएं

// विफल अपलोड पुनः प्रयास करें
actions.retry(); // सभी विफल पुनः प्रयास करें
actions.retry(['id1']); // विशिष्ट फाइलें पुनः प्रयास करें

// प्रोग्रामेटिकली फाइलें जोड़ें
actions.addFiles(fileList); // File[] या FileList जोड़ें

// फाइल मेटाडेटा अपडेट करें
actions.updateFileMeta('file-id', { tag: 'important' });

एक्शन मेथड्स

upload(fileIds?)

फाइलें अपलोड शुरू करें।

// सभी idle फाइलें अपलोड करें
actions.upload();

// विशिष्ट फाइलें अपलोड करें
actions.upload(['file-id-1', 'file-id-2']);
पैरामीटरप्रकारविवरण
fileIdsstring[] (वैकल्पिक)अपलोड करने के लिए विशिष्ट फाइल IDs

cancel(fileId?)

चल रहे अपलोड रद्द करें।

// सभी अपलोड रद्द करें
actions.cancel();

// विशिष्ट फाइल रद्द करें
actions.cancel('file-id');
पैरामीटरप्रकारविवरण
fileIdstring (वैकल्पिक)रद्द करने के लिए विशिष्ट फाइल ID

remove(fileId)

सूची से एक फाइल हटाएं।

actions.remove('file-id');
पैरामीटरप्रकारविवरण
fileIdstringहटाने के लिए फाइल ID

reset()

सभी फाइलें हटाएं और स्टेट रीसेट करें।

actions.reset();

retry(fileIds?)

विफल अपलोड पुनः प्रयास करें।

// सभी विफल अपलोड पुनः प्रयास करें
actions.retry();

// विशिष्ट फाइलें पुनः प्रयास करें
actions.retry(['file-id-1']);
पैरामीटरप्रकारविवरण
fileIdsstring[] (वैकल्पिक)पुनः प्रयास के लिए विशिष्ट फाइल IDs

addFiles(files)

प्रोग्रामेटिकली फाइलें जोड़ें।

// क्लिपबोर्ड से
document.addEventListener('paste', (e) => {
const files = e.clipboardData?.files;
if (files) {
actions.addFiles(files);
}
});

// अन्य स्रोत से
actions.addFiles([file1, file2]);
पैरामीटरप्रकारविवरण
filesFile[] | FileListजोड़ने के लिए फाइलें

updateFileMeta(fileId, meta)

फाइल मेटाडेटा अपडेट करें।

actions.updateFileMeta('file-id', {
description: 'मेरी फोटो',
tags: ['छुट्टी', '2024'],
});
पैरामीटरप्रकारविवरण
fileIdstringअपडेट करने के लिए फाइल ID
metaRecord<string, unknown>मर्ज करने के लिए मेटाडेटा

getDropProps

ड्रॉप जोन एलिमेंट पर स्प्रेड करने के लिए props लौटाता है।

const { getDropProps, getInputProps } = useDropup();

<div {...getDropProps()}>
<input {...getInputProps()} />
फाइलें यहां ड्रॉप करें
</div>

कस्टम Props के साथ

<div
{...getDropProps({
className: 'my-dropzone',
onClick: (e) => {
// आपका कस्टम क्लिक हैंडलर
console.log('क्लिक हुआ!');
},
})}
>
<input {...getInputProps()} />
</div>

लौटाए गए Props

गुणप्रकारविवरण
onDragEnterfunctionड्रैग एंटर हैंडलर
onDragOverfunctionड्रैग ओवर हैंडलर
onDragLeavefunctionड्रैग लीव हैंडलर
onDropfunctionड्रॉप हैंडलर
onClickfunctionक्लिक हैंडलर (डायलॉग खोलता है)
rolestringएक्सेसिबिलिटी रोल
tabIndexnumberफोकस के लिए टैब इंडेक्स

getInputProps

छिपे हुए फाइल इनपुट पर स्प्रेड करने के लिए props लौटाता है।

const { getInputProps } = useDropup();

<input {...getInputProps()} />

कस्टम Props के साथ

<input
{...getInputProps({
id: 'file-input',
name: 'files',
})}
/>

लौटाए गए Props

गुणप्रकारविवरण
type'file'इनपुट प्रकार
acceptstringस्वीकृत फाइल प्रकार
multiplebooleanएकाधिक फाइलों की अनुमति दें
onChangefunctionचेंज हैंडलर
styleobjectछिपे हुए स्टाइल्स

openFileDialog

प्रोग्रामेटिकली फाइल पिकर खोलने का फंक्शन।

const { openFileDialog } = useDropup();

<button onClick={openFileDialog}>
फाइलें ब्राउज़ करें
</button>

उपयोग उदाहरण

पूर्ण फाइल सूची

function FileList() {
const { files, actions, state } = useDropup({
upload: { url: '/api/upload' },
});

return (
<div>
<h3>फाइलें ({files.length})</h3>

{files.map(file => (
<div key={file.id}>
{file.preview && (
<img src={file.preview} alt="" width={50} />
)}
<span>{file.name}</span>
<span>{(file.size / 1024).toFixed(1)} KB</span>
<span>{file.status}</span>

{file.status === 'uploading' && (
<progress value={file.progress} max={100} />
)}

{file.status === 'error' && (
<>
<span style={{ color: 'red' }}>{file.error?.message}</span>
<button onClick={() => actions.retry([file.id])}>पुनः प्रयास</button>
</>
)}

<button onClick={() => actions.remove(file.id)}>हटाएं</button>
</div>
))}

{state.isUploading && (
<p>अपलोड हो रहा है... {state.progress}%</p>
)}

<button
onClick={() => actions.upload()}
disabled={state.isUploading || files.length === 0}
>
सभी अपलोड करें
</button>

<button onClick={() => actions.reset()}>
सभी साफ करें
</button>
</div>
);
}

ड्रैग स्टेट स्टाइलिंग

function StyledDropzone() {
const { state, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
});

const getStyle = () => {
if (state.isDragAccept) return { borderColor: 'green', backgroundColor: '#e8f5e9' };
if (state.isDragReject) return { borderColor: 'red', backgroundColor: '#ffebee' };
if (state.isDragging) return { borderColor: 'blue', backgroundColor: '#e3f2fd' };
return { borderColor: 'gray', backgroundColor: 'white' };
};

return (
<div
{...getDropProps()}
style={{
border: '2px dashed',
borderRadius: 8,
padding: 40,
textAlign: 'center',
transition: 'all 0.2s',
...getStyle(),
}}
>
<input {...getInputProps()} />
{state.isDragReject ? (
<p>केवल छवियां स्वीकार हैं!</p>
) : (
<p>छवियां यहां ड्रॉप करें</p>
)}
</div>
);
}