Search code examples
jqueryblueimpjquery-file-upload

Jquery file upload plugin: how to validate files on add?


$('#fileupload')
    .fileupload({
        acceptFileTypes: /(\.|\/)(jpg)$/i
    })
    .on('fileuploadadd', function (e, data) {
        console.log(data.files.valid); //undefined
        setTimeout(function () {
            console.log(data.files.valid); //true or false
        }, 500);
    })
;

jsFiddle

How to get boolean value of property data.files.valid without timeout ?


Solution

  • Here is what you want to do:

    $('#fileupload')
        .fileupload({
            acceptFileTypes: /(\.|\/)(jpg)$/i
        })
        .bind('fileuploadadded', function (e, data) {
            console.log(data.files.valid);
        });
    

    The core jquery.fileupload.js file is adding your files and triggering the "fileuploadadd" event, but that's before the files are validated.

    The file jquery.fileupload-ui.js is doing the validation after the files are added, and triggering another "fileuploadadded" event once that's done.

    Change the event, and you're all set.

    Please Note:

    This answer was valid and working as of March 2013 when it was posted. It appears that this upload plugin has changed since then. That means there is a new problem, and you should post a new question (or search to see if someone already has) rather than downvote this one!