Skip to content

Instantly share code, notes, and snippets.

@VicVicos
Created May 31, 2020 09:30
Show Gist options
  • Select an option

  • Save VicVicos/19333956eef839003c9f042f3470f5ec to your computer and use it in GitHub Desktop.

Select an option

Save VicVicos/19333956eef839003c9f042f3470f5ec to your computer and use it in GitHub Desktop.
XHR upload
function upload(url, data) {
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onload = xhr.onerror = function() {
if (this.status == 200) {
console.log("success");
} else {
console.log("error " + this.status);
}
};
xhr.upload.onprogress = function(event) {
console.log(event.loaded + ' / ' + event.total);
};
xhr.open("POST", window.location.origin + url, true);
// auth = "Basic " + btoa("log:pas");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// xhr.setRequestHeader("Content-type", "multipart/form-data");
// xhr.setRequestHeader('Authorization', auth);
// xhr.setRequestHeader('Access-Control-Allow-Origin', "*");
console.log(data);
xhr.send(data);
console.log(xhr);
}
var forms = document.forms;
[].forEach.call(forms, function(form, index) {
if (form.getAttribute('id') === "reviewCatalog") {
var oldForm = document.forms[index],
formData = new FormData(oldForm);
upload('/file.php', formData);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment