kiseikaijo-tools / index.html
soiz1's picture
Update index.html
ad6f0d1 verified
<!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 {
// URLからファイルを取得
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>