Search code examples
sveltesveltekit

SvelteKit - How to download a file from the assets?


I'm using SvelteKit and I have a file src/assets/my-file.pdf. I'm trying to make a button to download it:

<a download="my-file.pdf" target="_blank" href="assets/my-file.pdf">
    Download
</a>

But when I click on it, it doesn't work, I get the error File wasn't available on site.

How to download a file from the assets folder?


Solution

  • This is the way to use the assets in SvelteKit:

    <script>
        import MyFile from '$lib/assets/my-file.pdf';
    </script>
    
    <a download="my-file.pdf" target="_blank" href={MyFile}>
        Download
    </a>