24

And then alert the file name?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

37

This should suffice:

alert($('#your_input_box').val());

A file selection box is just an input box, from what I can tell. Read more from this question: jQuery: get the file name selected from <input type="file" />.

Community
  • 1
  • 1
Blender
  • 289,723
  • 53
  • 439
  • 496
21

To detect if a file was selected, you can find out the length of the file input

$("#bCheck").click(function() { // bCheck is a input type button
    var fileName = $("#file1").val();

    if(fileName) { // returns true if the string is not empty
        alert(fileName + " was selected");
    } else { // no file was selected
        alert("no file selected");
    }
});
  • Why does if(fileName) returns true? I just tested it and it works fine, I just don't understand why it returns true when the value is actual path of the file selected in string format. – Usce Oct 17 '18 at 11:21
  • 1
    @Usce if a string has a value it's truthy in javascript. – Sean T Nov 05 '18 at 11:08
  • Thanks for clarifying this topic @SeanT – Usce Nov 11 '18 at 15:24
  • This will not work as described. This is triggered when you click on the 'browse' button, and the value is checked BEFORE a file is selected. It will always come back with 'no file selected', unless a file was already selected. – Tom aka Sparky Aug 06 '21 at 00:24