Skip to content

Instantly share code, notes, and snippets.

@daoshengmu
Created May 18, 2020 07:52
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 daoshengmu/475e5e1de795dcb0796e0c055ec245d6 to your computer and use it in GitHub Desktop.
Save daoshengmu/475e5e1de795dcb0796e0c055ec245d6 to your computer and use it in GitHub Desktop.
XMLHttpRequest post image to S3
const url = "https://2r5x7bfm7h.execute-api.us-west-2.amazonaws.com/v1/upload";
const xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
xhr.open("POST", url, true);
} else if (typeof XDomainRequest != "undefined"){
xhr = new XDomainRequest();
xhr.open("POST", url);
} else {
xhr = null;
}
let self = this;
// xhr.setRequestHeader('Content-type', "application/x-amz-json-1.1");
xhr.setRequestHeader('Content-type', 'application/json');
xhr.onreadystatechange = function() {
// In local files, status is 0 upon success in Mozilla Firefox
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
console.log("Upload done. " + xhr.responseText);
self.setState({
s3ImageUrl: xhr.responseText
});
{ self.state.s3ImageUrl !== '' ? self.classifyImage() : '' };
} else {
console.log("Upload photo error.");
}
}
};
xhr.send(JSON.stringify({
"Image": this.state.base64String,
"name": "testImage.jpg"
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment