3

Is there any way to be able to detect with jQuery, when a file is uploaded to an input element like below:

<input type='file' name='filex' id='filex' style="height: 2.3em" size="23" />

I'm looking for an event specific to when the file is placed (uploaded) to the element, not when the element is clicked before the open file dialog is opened. I'm aware that I can submit the form and then get the file server side, but I want to display a thumbnail to the user before the form is submitted on confirm.

Thanks.

Mohammad Sepahvand
  • 16,889
  • 20
  • 78
  • 119

2 Answers2

3

As a file input will start off with no files selected, and no files can be selected without user-interaction, I'd suggest using the change() method (using the change event):

$('input:file').change(
    function(e){
        console.log('file "' + path.split('\\').pop(); + '" selected.');
    });

References:

Community
  • 1
  • 1
David says reinstate Monica
  • 230,743
  • 47
  • 350
  • 385
1
var fileInput = document.getElementById('filex');

fileInput.onchange = function(e){
    if(e.target.files.length > 0)
        // File uploaded
}
karaxuna
  • 25,822
  • 11
  • 76
  • 111