Create download-data shortcode

This commit is contained in:
Sam 2024-08-13 18:52:51 +01:00
parent 69158336bf
commit ed011152b2
1 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
<button class="download-button" onclick="downloadFile('{{ .Get "src" }}', '{{ .Get "name" }}')">
Download
</button>
<script>
async function downloadFile(url, name) {
try {
const response = await fetch(url);
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);
} catch (error) {
console.error('Error downloading the file:', error);
}
}
</script>