I am writing a script which is to upload files. I want to check if the file exists before the upload takes place, and then ultimatly will offer the user an overwrite option.
I have taken the code out of the normal flow to make sure another issue wasn't effecting it.
I am passing the filename to the function, which will use ajax to run a php script and return the result.
I want this result to return in the callback, and continue processing.
I have added alerts which show the order in which I expect them to fire (1,2,3), but they are firing in a different order (1,3,2)
Please help.
function Test() {
FileExists("Faces.png", function() {
alert("3");
});
}
function FileExists(Filename, callback)
{
var res;
var Obj = new Object();
Obj.function = 'FileExists';
Obj.Filename = Filename;
alert("1");
$.ajax({
url: "upload.php",
dataType: "text",
type: "POST",
data: Obj,
cache: false,
success: function (data) {
alert("2");
if (data == "1")
res = true;
else
res = false;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
res= false;
}
});
if(callback) callback(res);
}