File Downloads

Download a file, text, blob, etc programmatically with JavaScript, or download from a url without JavaScript

With JavaScript

export const download = (
content: string | Blob | File,
fileName: string,
type?: string
) => {
const blob =
typeof content === 'string' ? new Blob([content], { type }) : content
const a = document.createElement('a')
a.href = URL.createObjectURL(blob) = fileName
a.rel = 'noopener'
a.dispatchEvent(new MouseEvent('click'))


  • We use a.dispatchEvent over since some browsers don't start the download when is called.

Without JavaScript

<a href="{{url}}" download="{{fileName}}">Click me to download!</a>


  • The url must point to a resource on the same domain and should only contain the path. e.g. if the domain you're on is to download, the url should be /og.png


Download An Image (No JS)
Created 04/26/22Updated 10/13/22
Found a mistake, or want to suggest an improvement? Edit on GitHub here
and see edit history here