2

I have a page for allowing users to upload their images using drag and drop and I would like to restrict them to uploading jpg files only.

I have to admit I am a complete newbie when it comes to jquery so I really don't have a clue how to make this work.

So far I have

var files = e.dataTransfer.files;
var ext = $('#drop-files').val().split('.').pop().toLowerCase();


// Show the upload holder
$('#uploaded-holder').show();

// For each file
$.each(files, function(index, file) {
    if($.inArray(ext, ['JPG','jpg','jpeg']) == -1) {
           alert('invalid extension!');
           return false;
       }

As this is it does nothing, if I remove the 'return false' it tells me every file is invalid and uploads them anyway.

Thanks

btw, I am aware that there are unclosed brackets, I'm just hoping the someone will see something obvious I have missed out in the section of code I've supplied

3
  • If you really want to check the file type, read the payload using DataTransfer.getData() and check the leading bytes (PNG, GIF89, JFIF or something). source Commented Mar 5, 2013 at 10:55
  • @ShivanRaptor, nope, no error Commented Mar 5, 2013 at 11:08
  • @f00bar - thanks for that but I have absolutely no idea what that means! lol Commented Mar 5, 2013 at 11:09

1 Answer 1

2

The problem is because you are returning false from the function within the each loop, not the submit handler function. Try this:

// For each file
var validExtension = false;
$.each(files, function(index, file) {
    if ($.inArray(ext, ['JPG','jpg','jpeg']) != -1 && !validExtension) {
        validExtension = true;
    }
}
return validExtension;
Sign up to request clarification or add additional context in comments.

1 Comment

HI thanks for that, not sure why though, but it flags a syntax error after the return in Dreamweaver?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.