And then alert the file name?
Asked
Active
Viewed 5.7k times
24
-
1$('#inputfile').on('change', function (e) { alert(e.val()); }); – Hossin Asaadi Feb 02 '19 at 17:44
2 Answers
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" />.
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
-
-
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