Hi,
Hope this makes sense!
Basically I have a file control on a form and when a file is selected I want to check a few details and decide if the file is 'valid'
The initial function call needs a Boolean return value to decide if all was OK, but I can't get my head round how I do this when the code for a fileReader object and image object uses onload events.
Here is some code to hopefully explain better...
I call my function 'fileSelected' via a JQuery change event...
fileSelected does a few things such as check file type and file size, I then do the following validation
here is where I'm stuck for a start I know the 'return false' in the file.onload event is not returning to the fileSelected method, I doubt it's returning false to the fileReader reader.onload event either, how would you capture the return value of a bound function to an event handler?.
What I need after the 'read selected file' is to know was any of this OK, if so return true to the caller of fileSelected, otherwise false.
But I don't understand how I can do this?
I've googled and there is not a JS sleep/wait and apparently it's bad practice to try. I know there is some kind of when / deferred / promise in JQuery, but I could do with a hand getting my head round this if this is what I need to do.
How can you call a procedural function that needs to return a Boolean value when the function is creating event based actions and the procedural function ends before these events have finished but I don't want it to?
Advice appreciated.
1DMF
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
Hope this makes sense!
Basically I have a file control on a form and when a file is selected I want to check a few details and decide if the file is 'valid'
The initial function call needs a Boolean return value to decide if all was OK, but I can't get my head round how I do this when the code for a fileReader object and image object uses onload events.
Here is some code to hopefully explain better...
I call my function 'fileSelected' via a JQuery change event...
Code:
// upload file selected
$('body').on('change','#file',function() {
// validate selected file
[b]if(fileSelected( $(this), fileObj ))[/b]
{
// show selected file.
$(this).removeClass('invalid').addClass('valid');
displayBanner();
}
else
{
$(this).removeClass('valid').addClass('invalid');
}
});
fileSelected does a few things such as check file type and file size, I then do the following validation
Code:
// set up a fileReader object and image object
var reader = new FileReader();
fObj.file = new Image();
// load event to capture the file information.
reader.onload = (function(file) {
// load event to check image dimensions
fObj.file.onload = function(){
if(this.width != fObj.imgWidth || this.height != fObj.imgHeight)
{
showDialog({content:'Image dimensions invalid. Must be (Width: ' + fObj.imgWidth + 'px, Height:' + fObj.imgHeight + 'px)'});
clearFile(ele);
return false;
}
};
fObj.file.src = file.target.result;
});
// read selected file
reader.readAsDataURL(myFile);
// DO SOMETHING HERE TO CHECK AND RETURN TRUE OR FALSE
here is where I'm stuck for a start I know the 'return false' in the file.onload event is not returning to the fileSelected method, I doubt it's returning false to the fileReader reader.onload event either, how would you capture the return value of a bound function to an event handler?.
What I need after the 'read selected file' is to know was any of this OK, if so return true to the caller of fileSelected, otherwise false.
But I don't understand how I can do this?
I've googled and there is not a JS sleep/wait and apparently it's bad practice to try. I know there is some kind of when / deferred / promise in JQuery, but I could do with a hand getting my head round this if this is what I need to do.
How can you call a procedural function that needs to return a Boolean value when the function is creating event based actions and the procedural function ends before these events have finished but I don't want it to?
Advice appreciated.
1DMF
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music