4

This is my file input element:

<input type="file" id="StudentPhoto" value="StudentPhoto" style="display: none;">

This is the validation function:

$(document).ready(function() {
    $('#StudentPhoto').change(function()
    {
        var file_data = $("#StudentPhoto").prop("files")[0];
        var size = file_data.size;
        var type = file_data.type;
        var name = file_data.name;
        var input = $("#StudentPhoto");

        if(type !== 'image/png' && type !== 'image/jpg' && type !== 'image/jpeg')
        {
            alert("Only JPG & PNG files are allowed to upload as student photo.");
            $('#StudentPhoto').click();
        }
        else if(size > 2097152)
        {
            alert("The maximum photo size is 2MB\n");
            $('#StudentPhoto').click();
        }
    });
});

If the file chosen by the user has an invalid format or size, it's supposed to pop up the dialog to ask him to choose the file again, but it doesn't, the statement $('#StudentPhoto').click(); in the function doesn't work. Why? Is there any other method?

technophyle
  • 7,972
  • 6
  • 29
  • 50
Newbie
  • 1,584
  • 9
  • 33
  • 72

5 Answers5

0

Or you can use the .trigger() method to make the click event fire on the button. We'll also use input since you stored the file field already.

input.trigger('click');

Hope this helps.

James Pederson
  • 404
  • 2
  • 7
0

You can use HTML DOM click() method:

document.getElementById('StudentPhoto').click();
chungvh
  • 46
  • 1
  • 4
  • 1
    I found this answer. It may help you understand your problem. http://stackoverflow.com/a/25886585/3861639 – chungvh Aug 26 '15 at 03:27
  • @shess But `.click()` function for my file input works in other function/condition. – Newbie Aug 26 '15 at 03:50
  • 1
    this answer is not related. If jQuery works in other case, this traditional JS click is the same as jQuery version's `.click()`. – Raptor Aug 26 '15 at 03:57
0

Change your file input element to:

<input type="file" id="StudentPhoto" value="StudentPhoto" style="visibility: hidden">

Note that I used visibility: hidden, instead of display: none.

You cannot call the click event on a non-displayed file input.

technophyle
  • 7,972
  • 6
  • 29
  • 50
0

Since you are inside the change function and want to click on the same selector, you can use the this.click()

$(document).ready(function() {
    $('#StudentPhoto').change(function()
    {
        var file_data = $("#StudentPhoto").prop("files")[0];
        var size = file_data.size;
        var type = file_data.type;
        var name = file_data.name;
        var input = $("#StudentPhoto");

        if(type !== 'image/png' && type !== 'image/jpg' && type !== 'image/jpeg')
        {
            alert("Only JPG & PNG files are allowed to upload as student photo.");
            this.click();
        }
        else if(size > 2097152)
        {
            alert("The maximum photo size is 2MB\n");
            this.click();
        }
    });
});
Keshari Nandan
  • 1,040
  • 9
  • 22
-2

you need to insert your callback withing the click event:

    $(document).ready(function()
        {
            $('#StudentPhoto').change(function()
            { 
                var file_data = $("#StudentPhoto").prop("files")[0];
                var size = file_data.size;
                var type = file_data.type;
                var name = file_data.name;
                var input = $("#StudentPhoto");

                if(type !== 'image/png' && type !== 'image/jpg' && type !== 'image/jpeg')
                {
                    alert("Only JPG & PNG files are allowed to upload as student photo.");
                    $('#StudentPhoto').click();
                }
                else if(size > 2097152)
                {

                    $('#StudentPhoto').click(function(){
alert("The maximum photo size is 2MB\n");
});
                }
            });
    });
Leo Javier
  • 1,383
  • 12
  • 19