I am fairly new to the use of javascript and would appreciate any help. I have an application where the browser must use xmlhttprequest to receive a response from the server (true/false for testing purposes) and based on the response, the client will open a file selection dialog for the user to select a local file for uploading.
When I create the XMLHttpRequest with the async flag set to FALSE, when the client receives a "true" response from the server, a file selection dialog box opens (for both Chrome, IE).
When I create the XMLHttpRequest with the async flag set to TRUE ( as recommended), when the client receives a "true" response from the server, the same code path is followed, however a file selection dialog box never opens and no errors are displayed in the debuggers for Chrome, HOWEVER it still work in IE.
Here is the code:
...
// user has clicked button to upload a file
$scope.uploadFile = function () {
request = new XMLHttpRequest();
request.open("GET", "some-url", true);// false
request.onreadystatechange = $scope.checkUploadPermissions;
request.send();
}
// the callback
// If the user has permission (based on various factors not shown here)
// we open the dialog
// Otherwise we inform them that they are not allowed
$scope.checkUploadPermissions = function () {
// simplified for brevity
if (request.readyState == 4 && request.status == 200) {
// for this sample, we are only checking if the server returned true/false
var hasPerms = request.responseText;
if (hasPerms === "true") {
$scope.openFileSelector();
}
else {
alert("You do not have permission to upload a file.");
}
}
}
// if the user has permission to proceed, we trigger a click on a hidden element
$scope.openFileSelector = function () {
angular.element("#presentUpload").trigger("click");
}
...
I would like to reiterate that this code works perfectly when the async flag set to FALSE but not when it is set to TRUE.
How can I have this work properly when setting the flag to TRUE.
Thank you in advance.