0
function _validMimeType(file) {
        var validSignatures = ['ffd8ffe0', '89504e47', '424d3804', '49492a00', 
        '4d4d002a' , 'd0cf11e0', '504b0304', '74686973', '7b5c7274', '25504446', 
        '52617221'];
        var slice = file.slice(0, 4);  
        var reader = new FileReader();    
        reader.onloadend = function(e) {
            var view = new DataView(reader.result),
            signature;
            if(view.byteLength == 8){
                signature = view.getUint32(0, false).toString(16) + 
                view.getUint32(4, false).toString(16);
            } else {
               signature = view.getUint32(0, false).toString(16);
            }
            for (var i = 0; i < validSignatures.length; i++) {
               if (signature == validSignatures[i].toLowerCase()) {
                   console.log(signature);
                   return  true;
               }
           }
           return false;
        }
        reader.readAsArrayBuffer(slice); 
    }

_validMimeType(file) returns me undefined

When i pass file from a uploader to _validMimeType(file) it returns me undefined.

brk
  • 48,835
  • 10
  • 56
  • 78
Aatif
  • 11
  • 2
  • correct, because you are not returning anything in `_validMimeType` - and to pre-empt the next problem, read https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Jaromanda X Sep 27 '17 at 04:50
  • As this is not an ajax call. I need it to return true or false on onloadend function ends. How would I do that – Aatif Sep 27 '17 at 04:51
  • you can't make `_validMimeType` return a value synchronously because it determines the return value asynchronously ... you'll need to learn to use asynchronous techniques (callbacks, promises, etc) - did you read https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call? – Jaromanda X Sep 27 '17 at 04:52
  • I need to wait for file reader to return me true or false as undefined will not let me fire an ajax call. Can you help me with writing a callback function here – Aatif Sep 27 '17 at 04:56
  • `function _validMimeType(file, callback) {` then instead of `return true|false` do `return callback(true|false);` ... and then use it `_validMimeType(file, function(result) { ... do things with the result here ...})` – Jaromanda X Sep 27 '17 at 04:58

1 Answers1

0

Simplest solution, because FileReader is asynchronous, is to use a callback

function _validMimeType(file, callback) { // add callback argument
    var validSignatures = ['ffd8ffe0', '89504e47', '424d3804', '49492a00', 
    '4d4d002a' , 'd0cf11e0', '504b0304', '74686973', '7b5c7274', '25504446', 
    '52617221'];
    var slice = file.slice(0, 4);  
    var reader = new FileReader();    
    reader.onloadend = function(e) {
        var view = new DataView(reader.result),
        signature;
        if(view.byteLength == 8){
            signature = view.getUint32(0, false).toString(16) + 
            view.getUint32(4, false).toString(16);
        } else {
           signature = view.getUint32(0, false).toString(16);
        }
        for (var i = 0; i < validSignatures.length; i++) {
           if (signature == validSignatures[i].toLowerCase()) {
               console.log(signature);
               // change this
               return callback(true); // return so no fallthrough occurs
           }
       }
       // change this
       callback(false);
    }
    reader.readAsArrayBuffer(slice); 
}

_validMimeType(file, function(result) {
    // result is true or false
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87