Skip to content

Instantly share code, notes, and snippets.

@batrudinych
Created March 10, 2019 14:28
Show Gist options
  • Save batrudinych/d3a8fc3b3d4036840a939abd426bbc29 to your computer and use it in GitHub Desktop.
Save batrudinych/d3a8fc3b3d4036840a939abd426bbc29 to your computer and use it in GitHub Desktop.
Send a blob (which may be a file) from client app
// Blob may contain something else, check https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#Syntax
// Keep in mind this requires preflight
const blob = new Blob(['this is a test'], { type: 'application/x-www-form-urlencoded'})
const xhr = new XMLHttpRequest();
xhr.open("POST", 'http://localhost:8080/files', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && (this.status === 200 || this.status === 201)) {
console.log('Blob uploaded')
}
}
xhr.setRequestHeader('header-name', 'header-value');
xhr.send(blob);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment