Skip to content

Instantly share code, notes, and snippets.

@TrevorSundberg
Last active October 30, 2022 19:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TrevorSundberg/74dc4f576b94d19711a4547dcf04af40 to your computer and use it in GitHub Desktop.
Save TrevorSundberg/74dc4f576b94d19711a4547dcf04af40 to your computer and use it in GitHub Desktop.
Download a file in JavaScript with Emscripten / WebAssembly.
function download(filenamePtr, dataPtr, size) {
const a = document.createElement('a')
a.style = 'display:none'
document.body.appendChild(a)
const view = new Uint8Array(Module.HEAPU8.buffer, dataPtr, size)
const blob = new Blob([view], {
type: 'octet/stream'
})
const url = window.URL.createObjectURL(blob)
a.href = url
const filename = UTF8ToString(filenamePtr)
a.download = filename
a.click()
window.URL.revokeObjectURL(url)
document.body.removeChild(a)
}
window.download = download
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment