Skip to content

Instantly share code, notes, and snippets.

@panzi
Created November 1, 2012 05:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save panzi/3991951 to your computer and use it in GitHub Desktop.
Save panzi/3991951 to your computer and use it in GitHub Desktop.
Save/download data generated in JavaScript (2)
if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) {
  mimetype = "application/octet-stream";
 }
 
 blob = builder.getBlob(mimetype);
 url = URL.createObjectURL(blob);
 window.open(url, '_blank', '');
}
  setTimeout(function () {
   URL.revokeObjectURL(url);
  }, 250);
 };
}
else if (!/\bMSIE\b/.test(navigator.userAgent)) {
 showSave = function (data, name, mimetype) {
  if (!mimetype) mimetype = "application/octet-stream";
  // Again I need to filter the mime type so a download is forced.
  if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) {
   mimetype = "application/octet-stream";
  }
  window.open("data:"+mimetype+","+encodeURIComponent(data), '_blank', '');
 };
}
function saveData () {
 if (!showSave) {
  alert("Your browser does not support any method of saving JavaScript gnerated data to files.");
  return;
 }
 
 showSave(
  document.getElementById("data").value,
  document.getElementById("filename").value,
  document.getElementById("mimetype").value);
}
// ]]>
</script>
</head>
<body>
<label for="filename">File name:</label>
<input type="text" id="filename" value="Example.txt"/>
 
<label for="mimetype">Mime type:</label>
<input type="text" id="mimetype" value="text/plain; charset=UTF-8"/>
 
 
 
<label for="data">File contents:</label>
 
<textarea id="data">Multiline text containing unicode characters.
 
German umlauts: äöüÄÖÜ
Unicode quotes: »«„“”</textarea>
 
 
 
<button onclick="saveData();">Save Data</button>
</body>
</html>
var blob = new Blob([string],{type:mimetype});
@panzi
Copy link
Author

panzi commented Nov 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment