I'm trying to access the value of the variable ext
(defined within the function reader.onloadend
) in the function xhr.onreadystatechange
. Currently, it is returning undefined
. Does anyone have any suggestions on how to resolve this issue?
function process_file(file_input) {
var name = file_input.dataset.target;
var url = file_input.dataset.url;
var path = file_input.dataset.path;
var ext;
for(var i = 0; i<file_input.files.length; i++) {
var file = file_input.files[i];
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var id = xhr.responseText;
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", name);
input.setAttribute("value", id);
document.getElementById("btn_uload_"+name+"_"+ext).append(input);
document.getElementById("empty_file").style.display = 'none';
document.getElementById("single_file_"+ext).style.display = 'block';
}
};
var reader = new FileReader();
reader.onloadend = function() {
var bytes = reader.result;
ext = file.name.split(".").pop();
var formData = new FormData();
formData.append('bytes', bytes);
formData.append('type', ext);
xhr.send(formData);
}
reader.readAsDataURL(file);
}
}