Skip to content

Instantly share code, notes, and snippets.

@faveoled
Created June 12, 2023 13:28
Show Gist options
  • Save faveoled/8a47215339f5f05dfc6b56d291247818 to your computer and use it in GitHub Desktop.
Save faveoled/8a47215339f5f05dfc6b56d291247818 to your computer and use it in GitHub Desktop.
Scala.js - download browser-generated file
import org.scalajs.dom.document
import org.scalajs.dom.window
import org.scalajs.dom.URL
import org.scalajs.dom.Blob
def download(filename: String, contents: Blob): Unit = {
val elem = window.document.createElement("a").asInstanceOf[HTMLAnchorElement];
elem.href = URL.createObjectURL(contents);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment