Skip to content

Instantly share code, notes, and snippets.

@jens1101
Created May 22, 2018 08:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jens1101/0d6e5ff2c9582c86e5dbdb33d66777fd to your computer and use it in GitHub Desktop.
Save jens1101/0d6e5ff2c9582c86e5dbdb33d66777fd to your computer and use it in GitHub Desktop.
Save Blob as file in JavaScript
// In this file we use a data URL to represent a Blob. This basically base64
// encodes the Blob and puts that string in a URL. This has better compatibility
// with old browsers, but is limited to ~2MB.
const blob = getBlobFromSomewhere()
const reader = new FileReader()
reader.onload = function (event) {
const a = document.createElement('a')
a.href = event.target.result
a.download = 'example.xlsx'
a.click()
}
reader.readAsDataURL(blob)
// In this file we use an object URL to represent a Blob. Object URLs refer
// to a Blob or File in memory and are bound to the document they are
// created in. For details see:
// https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
// There doesn't seem to be a hard limit on the file size. The average limit
// seems to be ~600MiB
const blob = getBlobFromSomewhere()
const a = document.createElement('a')
a.href = window.URL.createObjectURL(blob)
a.download = 'example.xlsx'
a.click()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment