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

28 lines
871 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 {
2024-09-04 18:49:40 +01:00
downloadUrl = `${"{{ .Site.Params.apiURL }}"}/download${url}`
console.log("Downloading from url:",downloadUrl)
const response = await fetch(downloadUrl);
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>