Created
March 10, 2019 14:28
-
-
Save batrudinych/d3a8fc3b3d4036840a939abd426bbc29 to your computer and use it in GitHub Desktop.
Send a blob (which may be a file) from client app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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