|
|
<!DOCTYPE html> |
|
|
<html lang="ja"> |
|
|
<head> |
|
|
<meta charset="UTF-8"> |
|
|
<title>Fetch & Save File</title> |
|
|
</head> |
|
|
<body> |
|
|
<h1>URLからファイルをダウンロード</h1> |
|
|
<input type="text" id="fileUrl" placeholder="ファイルのURLを入力" size="50"> |
|
|
<button id="downloadBtn">ダウンロード</button> |
|
|
|
|
|
<script> |
|
|
const downloadBtn = document.getElementById('downloadBtn'); |
|
|
const fileUrlInput = document.getElementById('fileUrl'); |
|
|
|
|
|
downloadBtn.addEventListener('click', async () => { |
|
|
const url = fileUrlInput.value.trim(); |
|
|
if (!url) { |
|
|
alert('URLを入力してください'); |
|
|
return; |
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
const response = await fetch(url); |
|
|
if (!response.ok) throw new Error('ファイルの取得に失敗しました'); |
|
|
const blob = await response.blob(); |
|
|
|
|
|
|
|
|
const fileHandle = await window.showSaveFilePicker({ |
|
|
suggestedName: url.split('/').pop(), |
|
|
types: [ |
|
|
{ |
|
|
description: 'All Files', |
|
|
accept: { '*/*': ['.*'] }, |
|
|
}, |
|
|
], |
|
|
}); |
|
|
|
|
|
|
|
|
const writable = await fileHandle.createWritable(); |
|
|
await writable.write(blob); |
|
|
await writable.close(); |
|
|
|
|
|
alert('ダウンロード完了!'); |
|
|
} catch (err) { |
|
|
console.error(err); |
|
|
alert('エラー: ' + err.message); |
|
|
} |
|
|
}); |
|
|
</script> |
|
|
</body> |
|
|
</html> |
|
|
|