Optionsreferenz
Alle Konfigurationsoptionen für den useDropup Hook.
Validierungsoptionen
accept
Akzeptierte Dateitypen.
// Einzelner MIME-Typ
useDropup({ accept: 'image/png' });
// Mehrere MIME-Typen
useDropup({ accept: 'image/png, image/jpeg, image/gif' });
// Wildcard
useDropup({ accept: 'image/*' });
// Nach Erweiterung
useDropup({ accept: '.pdf, .doc, .docx' });
// Array-Format
useDropup({ accept: ['image/*', 'application/pdf', '.txt'] });
| Typ | Standard |
|---|---|
string | string[] | undefined (alle Dateien) |
maxSize
Maximale Dateigröße in Bytes.
useDropup({ maxSize: 10 * 1024 * 1024 }); // 10MB
| Typ | Standard |
|---|---|
number | undefined (kein Limit) |
minSize
Minimale Dateigröße in Bytes.
useDropup({ minSize: 1024 }); // 1KB Minimum
| Typ | Standard |
|---|---|
number | undefined (kein Limit) |
maxFiles
Maximale Anzahl erlaubter Dateien.
useDropup({ maxFiles: 5 });
| Typ | Standard |
|---|---|
number | undefined (kein Limit) |
maxWidth / maxHeight
Maximale Bildabmessungen in Pixeln.
useDropup({
accept: 'image/*',
maxWidth: 4096,
maxHeight: 4096,
});
| Typ | Standard |
|---|---|
number | undefined (kein Limit) |
minWidth / minHeight
Minimale Bildabmessungen in Pixeln.
useDropup({
accept: 'image/*',
minWidth: 100,
minHeight: 100,
});
| Typ | Standard |
|---|---|
number | undefined (kein Limit) |
customRules
Benutzerdefinierte Validierungsfunktionen.
useDropup({
customRules: [
// Synchrone Validierung
(file) => {
if (file.name.includes('draft')) {
return 'Entwurfsdateien nicht erlaubt';
}
return true;
},
// Asynchrone Validierung
async (file) => {
const exists = await checkServerDuplicate(file);
return exists ? 'Datei existiert bereits' : true;
},
],
});
| Typ | Standard |
|---|---|
ValidationRule[] | [] |
ValidationRule-Typ:
type ValidationRule = (file: File) => boolean | string | Promise<boolean | string>;
Verhaltensoptionen
multiple
Mehrfachauswahl von Dateien erlauben.
useDropup({ multiple: true }); // Mehrere Dateien
useDropup({ multiple: false }); // Nur eine Datei
| Typ | Standard |
|---|---|
boolean | true |
disabled
Ablagezone deaktivieren.
useDropup({ disabled: true });
| Typ | Standard |
|---|---|
boolean | false |
autoUpload
Upload automatisch starten, wenn Dateien hinzugefügt werden.
useDropup({
upload: { url: '/api/upload' },
autoUpload: true,
});
| Typ | Standard |
|---|---|
boolean | false |
generatePreviews
Vorschau-URLs für Bilddateien generieren.
useDropup({ generatePreviews: true });
// Auf Vorschau zugreifen
files[0].preview // "blob:http://..."
| Typ | Standard |
|---|---|
boolean | true |
Upload-Konfiguration
upload
Konfigurieren Sie das Upload-Ziel und -Verhalten.
URL-basierte Konfiguration
useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': 'Bearer token',
},
fieldName: 'file',
withCredentials: true,
timeout: 30000,
},
});
UploadConfig-Eigenschaften:
| Eigenschaft | Typ | Standard | Beschreibung |
|---|---|---|---|
url | string | Erforderlich | Upload-Endpunkt-URL |
method | string | 'POST' | HTTP-Methode |
headers | Record<string, string> | {} | Request-Header |
fieldName | string | 'file' | Formularfeldname für Datei |
withCredentials | boolean | false | Cookies einschließen |
timeout | number | 0 | Request-Timeout (ms) |
data | Record<string, unknown> | {} | Zusätzliche Formulardaten |
Benutzerdefinierter Uploader
useDropup({
upload: async (file, options) => {
const formData = new FormData();
formData.append('file', file.file);
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
signal: options.signal,
});
const data = await response.json();
return { url: data.url };
},
});
CustomUploader-Typ:
type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;
interface UploadOptions {
signal: AbortSignal;
onProgress: (progress: number) => void;
}
interface UploadResult {
url?: string;
response?: unknown;
}
Cloud-Uploader
import { createS3Uploader } from '@samithahansaka/dropup/cloud/s3';
useDropup({
upload: createS3Uploader({
getPresignedUrl: async (file) => {
const res = await fetch('/api/s3-presign', {
method: 'POST',
body: JSON.stringify({ filename: file.name, type: file.type }),
});
return res.json();
},
}),
});
Callback-Optionen
onFilesAdded
Wird aufgerufen, wenn Dateien hinzugefügt werden (nach Validierung).
useDropup({
onFilesAdded: (files) => {
console.log('Hinzugefügt:', files.map(f => f.name));
},
});
| Typ |
|---|
(files: DropupFile[]) => void |
onFileRemoved
Wird aufgerufen, wenn eine Datei entfernt wird.
useDropup({
onFileRemoved: (file) => {
console.log('Entfernt:', file.name);
},
});
| Typ |
|---|
(file: DropupFile) => void |
onValidationError
Wird aufgerufen, wenn Dateien die Validierung nicht bestehen.
useDropup({
accept: 'image/*',
onValidationError: (errors) => {
errors.forEach(({ file, errors }) => {
console.log(`${file.name}: ${errors.join(', ')}`);
});
},
});
| Typ |
|---|
(errors: ValidationError[]) => void |
onUploadStart
Wird aufgerufen, wenn eine Datei mit dem Upload beginnt.
useDropup({
onUploadStart: (file) => {
console.log('Upload gestartet:', file.name);
},
});
| Typ |
|---|
(file: DropupFile) => void |
onUploadProgress
Wird während des Upload-Fortschritts aufgerufen.
useDropup({
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
});
| Typ |
|---|
(file: DropupFile, progress: number) => void |
onUploadComplete
Wird aufgerufen, wenn ein Datei-Upload abgeschlossen ist.
useDropup({
onUploadComplete: (file, response) => {
console.log('Hochgeladen:', file.uploadedUrl);
console.log('Server-Antwort:', response);
},
});
| Typ |
|---|
(file: DropupFile, response: unknown) => void |
onUploadError
Wird aufgerufen, wenn ein Datei-Upload fehlschlägt.
useDropup({
onUploadError: (file, error) => {
console.error(`Fehlgeschlagen: ${file.name}`, error.message);
},
});
| Typ |
|---|
(file: DropupFile, error: DropupError) => void |
onAllComplete
Wird aufgerufen, wenn alle Dateien hochgeladen sind.
useDropup({
onAllComplete: (files) => {
const successful = files.filter(f => f.status === 'complete');
console.log(`${successful.length}/${files.length} erfolgreich hochgeladen`);
},
});
| Typ |
|---|
(files: DropupFile[]) => void |
Vollständiges Beispiel
const { files, actions, state, getDropProps, getInputProps } = useDropup({
// Validierung
accept: ['image/*', 'application/pdf'],
maxSize: 10 * 1024 * 1024,
maxFiles: 5,
// Verhalten
multiple: true,
autoUpload: false,
generatePreviews: true,
// Upload
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
},
},
// Callbacks
onFilesAdded: (files) => {
toast.info(`${files.length} Datei(en) hinzugefügt`);
},
onValidationError: (errors) => {
toast.error(`${errors.length} Datei(en) abgelehnt`);
},
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
onUploadComplete: (file) => {
toast.success(`${file.name} hochgeladen!`);
},
onUploadError: (file, error) => {
toast.error(`${file.name} fehlgeschlagen: ${error.message}`);
},
onAllComplete: (files) => {
const count = files.filter(f => f.status === 'complete').length;
toast.success(`Fertig! ${count} Dateien hochgeladen.`);
},
});