baseddata.io/layouts/shortcodes/download-data.html

26 lines
740 B
HTML
Raw Normal View History

2024-08-13 18:52:51 +01:00
<button class="download-button" onclick="downloadFile('{{ .Get "src" }}', '{{ .Get "name" }}')">
Download
</button>
<script>
async function downloadFile(url, name) {
try {
const response = await fetch(url);
2024-08-13 20:14:17 +01:00
if (!response.ok) {
throw new Error('Network response was not ok');
}
2024-08-13 18:52:51 +01:00
const blob = await response.blob();
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
2024-08-13 20:14:17 +01:00
URL.revokeObjectURL(link.href); // Clean up the object URL
2024-08-13 18:52:51 +01:00
} catch (error) {
console.error('Error downloading the file:', error);
}
}
</script>