Skip to content

Instantly share code, notes, and snippets.

@yig
Last active December 7, 2015 02:52
Show Gist options
  • Select an option

  • Save yig/aeeb1ee67a13bea98f3d to your computer and use it in GitHub Desktop.

Select an option

Save yig/aeeb1ee67a13bea98f3d to your computer and use it in GitHub Desktop.
Simple implementation of saveAs from the withdrawn HTML5 FileSaver API. It's a baby version of eligrey/FileSaver.js.
function saveAs( blob, name ) {
"use strict";
// Inspired by Syntax: http://stackoverflow.com/questions/23451726/saving-binary-data-as-file-using-javascript-from-a-browser/23451803#23451803
// Initially created to work around a bug in eligray/FileSaver.js
// which prevented saving multiple files
// (Issue 165: https://github.com/eligrey/FileSaver.js/issues/165 ).
// Create a hidden `a` element.
var a = document.createElement("a");
document.body.appendChild(a);
a.style.cssText = "display: none";
// createObjectURL() will leak memory.
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name;
a.click();
window.URL.revokeObjectURL(url);
a.parentNode.removeChild(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment