Created
February 10, 2016 22:24
-
-
Save vblagoje/fa653a62a1754ac123a0 to your computer and use it in GitHub Desktop.
Upload file to Wildfly using XMLHttpRequest
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
ModelControllerClient.prototype.uploadFile = function(op, file, uploadProgress) { | |
var fd = new FormData(); | |
fd.append('file', file); | |
var blob = new Blob([JSON.stringify(op)], {type : "application/json"}); | |
fd.append('operation', blob); | |
console.log('Upload started'); | |
var http = new XMLHttpRequest(); | |
http.upload.addEventListener("progress", uploadProgress, false); | |
http.addEventListener("readystatechange", function (e) { | |
// upload completed | |
if (this.readyState === 4) { | |
console.log('Success: Upload done', e); | |
var response = e.target.response; | |
if (response) { | |
try { | |
console.log("Got response " + response); | |
response = JSON.parse(response); | |
} catch (e) { | |
console.log('JSON.parse()', e); | |
} | |
} | |
//callback(false, response); | |
} | |
}); | |
http.addEventListener("error", function (e) { | |
console.log('Error: Upload failed', e); | |
}); | |
if (this.credentials.username) { | |
http.withCredentials = true; | |
http.open('POST', this.uploadUrl, true, this.credentials.username, this.credentials.password); | |
} else { | |
http.open('POST', this.uploadUrl, true); | |
} | |
//special headers to prevent CSRF | |
http.setRequestHeader('X-Management-Client-Name', 'HAL'); | |
// send the form data to the server | |
http.send(fd); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment