Last active
October 30, 2022 19:55
-
-
Save TrevorSundberg/74dc4f576b94d19711a4547dcf04af40 to your computer and use it in GitHub Desktop.
Download a file in JavaScript with Emscripten / WebAssembly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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