跳至主要內容

基本範例

使用 Dropup 最簡單的方式。

最小設定

import { useDropup } from '@samithahansaka/dropup';

function BasicUploader() {
const { files, getDropProps, getInputProps } = useDropup();

return (
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center',
cursor: 'pointer',
}}
>
<input {...getInputProps()} />
<p>將檔案拖放到此處或點擊選擇</p>

{files.length > 0 && (
<ul style={{ listStyle: 'none', padding: 0, marginTop: 20 }}>
{files.map(file => (
<li key={file.id}>
{file.name} ({(file.size / 1024).toFixed(1)} KB)
</li>
))}
</ul>
)}
</div>
);
}

帶上傳功能

import { useDropup } from '@samithahansaka/dropup';

function BasicUploadWithSubmit() {
const {
files,
actions,
state,
getDropProps,
getInputProps,
} = useDropup({
upload: {
url: '/api/upload',
method: 'POST',
},
});

return (
<div>
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center',
}}
>
<input {...getInputProps()} />
<p>將檔案拖放到此處或點擊選擇</p>
</div>

{/* 檔案列表 */}
{files.length > 0 && (
<div style={{ marginTop: 20 }}>
<h3>已選擇的檔案:</h3>
{files.map(file => (
<div
key={file.id}
style={{
display: 'flex',
justifyContent: 'space-between',
padding: 10,
borderBottom: '1px solid #eee',
}}
>
<span>{file.name}</span>
<span>{file.status}</span>
<button onClick={() => actions.remove(file.id)}>移除</button>
</div>
))}
</div>
)}

{/* 上傳按鈕 */}
<div style={{ marginTop: 20 }}>
<button
onClick={() => actions.upload()}
disabled={files.length === 0 || state.isUploading}
>
{state.isUploading ? `上傳中... ${state.progress}%` : '上傳全部'}
</button>

<button onClick={() => actions.reset()} style={{ marginLeft: 10 }}>
清除全部
</button>
</div>
</div>
);
}

單檔上傳

import { useDropup } from '@samithahansaka/dropup';

function SingleFileUploader() {
const { files, getDropProps, getInputProps } = useDropup({
multiple: false, // 只允許一個檔案
maxFiles: 1,
});

const file = files[0];

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
{file ? (
<p>已選擇:{file.name}</p>
) : (
<p>將檔案拖放到此處</p>
)}
</div>
);
}

const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
},
};

限制檔案類型

import { useDropup } from '@samithahansaka/dropup';

function ImageOnlyUploader() {
const { files, state, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
onValidationError: (errors) => {
errors.forEach(({ file }) => {
alert(`${file.name} 不是有效的圖片`);
});
},
});

return (
<div
{...getDropProps()}
style={{
...styles.dropzone,
borderColor: state.isDragReject ? 'red' : '#ccc',
backgroundColor: state.isDragReject ? '#fff0f0' : 'white',
}}
>
<input {...getInputProps()} />
{state.isDragReject ? (
<p style={{ color: 'red' }}>只接受圖片!</p>
) : (
<p>將圖片拖放到此處</p>
)}
</div>
);
}

使用回呼

import { useDropup } from '@samithahansaka/dropup';

function UploaderWithCallbacks() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },

onFilesAdded: (newFiles) => {
console.log('已新增檔案:', newFiles.map(f => f.name));
},

onUploadStart: (file) => {
console.log('開始上傳:', file.name);
},

onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},

onUploadComplete: (file, response) => {
console.log('完成:', file.name, file.uploadedUrl);
},

onUploadError: (file, error) => {
console.error('失敗:', file.name, error.message);
},

onAllComplete: (allFiles) => {
const successful = allFiles.filter(f => f.status === 'complete');
console.log(`完成!${successful.length}/${allFiles.length} 個成功`);
},
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>將檔案拖放到此處</p>
</div>

<button onClick={() => actions.upload()}>
上傳({files.length} 個檔案)
</button>
</div>
);
}

獨立按鈕觸發

import { useDropup } from '@samithahansaka/dropup';

function SeparateButtonUploader() {
const {
files,
openFileDialog,
getDropProps,
getInputProps,
} = useDropup();

return (
<div>
{/* 隱藏的拖放區域仍然接受拖放 */}
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
padding: 20,
marginBottom: 10,
}}
>
<input {...getInputProps()} />
<p>將檔案拖放到此處</p>
</div>

{/* 獨立按鈕開啟檔案對話框 */}
<button onClick={openFileDialog}>
瀏覽檔案
</button>

{files.length > 0 && (
<p>已選擇 {files.length} 個檔案</p>
)}
</div>
);
}

自動上傳

import { useDropup } from '@samithahansaka/dropup';

function AutoUploader() {
const { files, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
autoUpload: true, // 檔案新增時自動上傳
onUploadComplete: (file) => {
console.log('已上傳:', file.uploadedUrl);
},
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>將檔案拖放到此處 - 會自動上傳!</p>

{files.map(file => (
<div key={file.id}>
{file.name}:
{file.status === 'uploading' && ` ${file.progress}%`}
{file.status === 'complete' && ' 完成!'}
{file.status === 'error' && ` 錯誤: ${file.error?.message}`}
</div>
))}
</div>
);
}

下一步