Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created March 15, 2025 01:53
Show Gist options
  • Save wesleybliss/2237998a165962dfbf35d633369e32bd to your computer and use it in GitHub Desktop.
Save wesleybliss/2237998a165962dfbf35d633369e32bd to your computer and use it in GitHub Desktop.
Download a string as a file
const downloadCsv = (text, filename, encoding = 'text/plain') => {
// text/csv
const blob = new Blob([csvString], { type: encoding })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.setAttribute('href', url)
a.setAttribute('download', filename || 'download.txt')
a.style.display = 'none'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment