Skip to content

Instantly share code, notes, and snippets.

@fysherman
Created November 15, 2021 06:54
Show Gist options
  • Save fysherman/ad82ca412b9e3bc42f8b8ef7b7526c7f to your computer and use it in GitHub Desktop.
Save fysherman/ad82ca412b9e3bc42f8b8ef7b7526c7f to your computer and use it in GitHub Desktop.
Convert data to file and download in browser
//data: file data
// fileName: file's name to download
// typeFile: type file download. eg: text/html
function downloadFile(data, fileName, typeFile) {
const aEl = document.createElement('a')
const file = new Blob([data], { type: typeFile})
aEl.href = URL.createObjectURL(file)
aEl.download = fileName
aEl.click()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment