Skip to content

Instantly share code, notes, and snippets.

@guysmoilov

guysmoilov/save-js-blob.js

Last active Jul 30, 2017
Embed
What would you like to do?
Saves an octet-stream returned from a REST call as a file
var toSend = {};
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "https://myserver.com/path", true);
xmlhttp.responseType="blob";
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.overrideMimeType('application/octet-stream');
xmlhttp.onload = function() {
console.log('success');
console.log(xmlhttp.response);
window.open(URL.createObjectURL(xmlhttp.response)).focus();
};
xmlhttp.send(JSON.stringify(toSend));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment