📥 Add functionality to download individual and multiple files from the server

This commit is contained in:
2026-01-28 19:10:07 +00:00
parent a169b1e698
commit ac76ef108b

View File

@@ -131,7 +131,11 @@
</button>
</div>
</div>
<div v-if="serverStatus.totalFiles > 0" class="button-group" style="margin-top: 1rem;">
<div
v-if="serverStatus.totalFiles > 0"
class="button-group"
style="margin-top: 1rem"
>
<button @click="downloadAllFromServer" class="btn btn-primary">
📦 Download All from Server
</button>
@@ -286,7 +290,7 @@ async function saveToServer() {
const result = await response.json();
saveSuccess.value = true;
console.log('✅ Files saved to server:', result);
// Reload server status
await loadServerStatus();
} catch (err) {
@@ -317,6 +321,41 @@ function downloadAll() {
setTimeout(() => downloadMoves(), 1000);
}
async function downloadFromServer(filename) {
try {
const response = await fetch(`/api/gamemaster/download/${filename}`);
if (!response.ok) {
throw new Error(`Failed to download ${filename}`);
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} catch (err) {
error.value = `Failed to download ${filename}: ${err.message}`;
}
}
async function downloadAllFromServer() {
const filesToDownload = [
'pokemon.json',
'pokemon-allFormsCostumes.json',
'pokemon-moves.json'
];
for (let i = 0; i < filesToDownload.length; i++) {
setTimeout(() => {
downloadFromServer(filesToDownload[i]);
}, i * 500);
}
}
function formatDate(dateString) {
if (!dateString || dateString === 'Never') return 'Never';
return new Date(dateString).toLocaleString();