Skip to content

Instantly share code, notes, and snippets.

@nilcolor
Created July 3, 2012 11:14
Show Gist options
  • Save nilcolor/3039133 to your computer and use it in GitHub Desktop.
Save nilcolor/3039133 to your computer and use it in GitHub Desktop.
Upload file using FormData (a-la AJAX file upload using XMLHTTPRequest2)
function sendForm() {
var fileData = new FormData(document.getElementById("cpd-file"));
var xhr = new XMLHttpRequest();
xhr.open("POST", "/upload_file", true);
xhr.setRequestHeader('Accept', 'application/json');
xhr.onload = function(progressEvent) {
if (xhr.status == 200) {
console.log('Uploaded', xhr);
} else {
console.log("Error " + xhr.status + " occurred uploading your file.", xhr);
}
};
xhr.send(oData);
}
<form id="cpd-file" action="/upload_file" method="post" enctype="multipart/form-data">
<input type="file" name="cpd_file">
</form>
@spir1donov
Copy link

Thank you! I’ll make a good use of this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment