I want to get the file name of the uploaded file using jQuery. But the problem, I got the fake path and file name, instead of the file name only. This is my form.
<form action="" method="post">
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" name="submit" value="Upload" id="inputSubmit" />
</form>
And the jQuery is like this
$(document).ready(function(){
$('#inputSubmit').click(function(){
alert($('#uploadFile').val());
});
});
I use Chrome and got this in the alert box. Let's say I choose file named filename.jpg.
C:\fakepath\filename.jpg
How can I get only the file name? Thank you for your help.
To select the file we will use HTML <input type=”file”>. After that we will get the file name by using the jQuery change() method. This method is used in the JQuery to get the file input by selected file name.
If you go to Internet Explorer; Tools; Internet Option; Security; Custom; find the "Include local directory path when uploading files to a server" (it is quite a ways down) and click on "Enable" You may have to restart your computer, but this works & eliminates that "fakepath" that inhibits the upload file path.
length property in jQuery to check the file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.
How do I get the input file name in React? React. js component : Click on the choose file button, select one file and it will print the details on console. We are using one loop and printing the following file properties : File.name : It returns the name of the file.
Split the filepath string with "\" and use the last item in the resulting array.
see: Getting the last element of a split string array
You can also do:
$('#uploadFile').prop("files")['name'];
If it's a multiple file input, do:
$.each($('#uploadFile').prop("files"), function(k,v){
var filename = v['name'];
// filename = "blahblah.jpg", without path
});
get file name when uploading using jquery
var filename = $('input[type=file]').val().split('\').pop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With