Skip to content

Instantly share code, notes, and snippets.

@n4bb12
Last active January 16, 2023 05:12
Show Gist options
  • Save n4bb12/d44beb4a7080aae51136349cb7f7954f to your computer and use it in GitHub Desktop.
Save n4bb12/d44beb4a7080aae51136349cb7f7954f to your computer and use it in GitHub Desktop.
Download a in-memory content with a specific file type
export function download({
filename,
content,
contentType,
}: {
content: string
contentType: string
filename: string
}) {
const a = document.createElement("a")
const file = new Blob([content], { type: contentType })
const url = URL.createObjectURL(file)
a.href = url
a.download = filename
a.click()
setTimeout(() => {
URL.revokeObjectURL(url)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment