Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Created October 16, 2018 13:23
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 jastisriradheshyam/f8775401481a3d87d3f2e17ad53bd306 to your computer and use it in GitHub Desktop.
Save jastisriradheshyam/f8775401481a3d87d3f2e17ad53bd306 to your computer and use it in GitHub Desktop.
Document in upload
var someFunctionToUse = function (obj) {
// Form data
var formData = new FormData();
// Appending the files data in form data
formData.append("doc_file", document.getElementById("upload_input_id").files[0]);
// (Example) attribute from an object
formData.append("data_filed_name", obj.getAttribute("object_attribute_name"));
// Creating the Request object
var oReq = new XMLHttpRequest();
// Creating event
oReq.onreadystatechange = function () {
// Checking the ready state and response
if (this.readyState == 4 && this.status == 200) {
let response = JSON.parse(this.response);
// Checking the resposne (Example)
if (response["status"] == "success") {
location.reload();
} else {
console.log(response, " ", response.status)
}
}
};
// Creating the POST request and with async
oReq.open("POST", "/someURL", true);
// Sending the request
oReq.send(formData);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment