Skip to content

Instantly share code, notes, and snippets.

@guysmoilov
Last active July 30, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guysmoilov/9098eba0f9d777a1ed8320d4ba165b7f to your computer and use it in GitHub Desktop.
Save guysmoilov/9098eba0f9d777a1ed8320d4ba165b7f to your computer and use it in GitHub Desktop.
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