Created
May 31, 2020 09:30
-
-
Save VicVicos/19333956eef839003c9f042f3470f5ec to your computer and use it in GitHub Desktop.
XHR upload
This file contains hidden or 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
| 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