ファイルアップロード File Upload

ファイルをドラッグ&ドロップまたはクリックでアップロードする領域。

使用場面: 画像アップロード、ドキュメント添付、プロフィール写真の設定 採用例: Dropbox, Google Drive

ライブデモ

ファイルをドラッグ&ドロップ

またはファイルを選択

ソースコード

<div class="demo-upload">
  <div class="demo-upload-zone" tabindex="0">
    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" class="demo-upload-icon"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
    <p class="demo-upload-text">ファイルをドラッグ&ドロップ</p>
    <p class="demo-upload-hint">または<span class="demo-upload-browse">ファイルを選択</span></p>
    <input type="file" class="demo-upload-input" multiple accept="image/*,.pdf" />
  </div>
  <div class="demo-upload-files"></div>
</div>
.demo-upload { max-width: 400px; }
.demo-upload-zone {
  border: 2px dashed #d1d5db; border-radius: 12px;
  padding: 32px 20px; text-align: center; cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.demo-upload-zone:hover, .demo-upload-zone.dragover {
  border-color: var(--ui-primary);
  background: color-mix(in srgb, var(--ui-primary) 5%, transparent);
}
.demo-upload-icon { color: #999; margin-bottom: 8px; }
.demo-upload-zone:hover .demo-upload-icon,
.demo-upload-zone.dragover .demo-upload-icon { color: var(--ui-primary); }
.demo-upload-text { font-size: 14px; color: #555; margin: 0 0 4px; }
.demo-upload-hint { font-size: 12px; color: #999; margin: 0; }
.demo-upload-browse { color: var(--ui-primary); font-weight: 600; }
.demo-upload-input { display: none; }
.demo-upload-files { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.demo-upload-file {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; background: #f9fafb; border-radius: 8px; font-size: 13px;
}
.demo-upload-file-name { flex: 1; color: #333; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.demo-upload-file-size { color: #999; font-size: 11px; }
.demo-upload-file-remove { background: none; border: none; color: #999; cursor: pointer; font-size: 16px; }
.demo-upload-file-remove:hover { color: #ef4444; }
(function(){
  const zone = document.querySelector('.demo-upload-zone');
  const input = document.querySelector('.demo-upload-input');
  const filesEl = document.querySelector('.demo-upload-files');
  zone.addEventListener('click', () => input.click());
  zone.addEventListener('dragover', (e) => { e.preventDefault(); zone.classList.add('dragover'); });
  zone.addEventListener('dragleave', () => zone.classList.remove('dragover'));
  zone.addEventListener('drop', (e) => { e.preventDefault(); zone.classList.remove('dragover'); showFiles(e.dataTransfer.files); });
  input.addEventListener('change', () => showFiles(input.files));
  function showFiles(files) {
    filesEl.innerHTML = '';
    Array.from(files).forEach(f => {
      const size = f.size < 1024*1024 ? (f.size/1024).toFixed(1)+'KB' : (f.size/1024/1024).toFixed(1)+'MB';
      const div = document.createElement('div');
      div.className = 'demo-upload-file';
      div.innerHTML = '<span class="demo-upload-file-name">'+f.name+'</span><span class="demo-upload-file-size">'+size+'</span><button class="demo-upload-file-remove">&times;</button>';
      div.querySelector('.demo-upload-file-remove').addEventListener('click', () => div.remove());
      filesEl.appendChild(div);
    });
  }
})();

AIプロンプト

Basic
ファイルアップロードUIをHTML/CSS/JSで作ってください。ドラッグ&ドロップとクリックの両方でファイルを選択できます。
Custom
以下の仕様でファイルアップロードを実装してください。
- ドラッグ&ドロップエリアとファイル選択ボタン
- プライマリカラー: #2563eb
- アップロード進捗バー表示
- ファイルプレビュー(画像はサムネイル表示)
- ファイルサイズ制限(5MB)
- ファイル形式制限(画像のみ)
- ファイル削除ボタン
Advanced
本格的なファイルアップロードを実装してください。
- チャンクアップロード(大容量ファイル対応)
- 複数ファイルの並列アップロード
- 進捗表示(個別 + 全体)
- リトライ機能(失敗時の再アップロード)
- ファイルバリデーション(サイズ、形式、画像の解像度)
- ドラッグ&ドロップ時のファイル種別プレビュー
- aria-label, aria-describedby でアクセシビリティ対応
- キーボードでの操作(Enter/Spaceでファイル選択ダイアログ)
- ServiceWorkerでバックグラウンドアップロード対応