1

I want to get the filename of a file user selected when a user clicks on a button it is not a submit button. I have tried many of the solutions given on stackoverflow but it does not work i have id of the field but it does not give the name of the file.

Below is my code:

function Data(a)
{   
var ext = $('#fieldid_'+a).val().split('.').pop().toLowerCase();
.......

<input type="file" name="save_<?=$row1['id']?>" id="fieldid_<?=$row1['id']?>" /> 
<input type="button" id="submit_<?=$row1['id']?>" value="Upload File" onclick = "return Data(<?=$row1['id']?>)"/> 
4
  • What does it give you? Pop removes the last element from the array so you are grabbing the extension of the file. Commented Oct 29, 2015 at 12:50
  • @epascarello blank value, i have also checked it with $('#fieldid_'+a).val() but the same blank value is the output. Commented Oct 29, 2015 at 12:51
  • So I ran the code with no issues, it returns the extension. So time to debug. What does console.log( $('#fieldid_'+a)); return? My guess it is empty meaning it is not finding the element. Commented Oct 29, 2015 at 12:54
  • Please put the generated source code and not the php mark up in your question. There is something wrong with selecting the element. Commented Oct 29, 2015 at 13:04

3 Answers 3

1

Use the below script to get the file name: Reference Link:Use jQuery to get the file input's selected filename without the path

jQuery('input[type=file]')[0].files[0].name

(OR)

$('input[type=file]')[0].files[0].name

For more than on file:

var all_files = jQuery('input[type=file]');
var paths_arr = [];
for(var i=0;i<all_files.length;i++){
paths_arr.push(all_files[i].files[0].name);
} 
 paths_arr[0] = first file name
 paths_arr[1] = second file name
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer but it is giving me the name of file only of the first file control and i want the name of file which i pass as an id
var all_files = jQuery('input[type=file]'); var paths_arr = []; for(var i=0;i<all_files.length;i++){ paths_arr.push(all_files[i].files[0].name); you can access the file names like all_files[0] = first file,all_files[1] second file } @user3653474
0
document.getElementById('fileInputId').files[0].name

see fiddle

2 Comments

This didn't work. It is giving following error Uncaught TypeError: Cannot read property 'files' of null
"How to get the filename from file input type with jquery without form submit"
0

Files is the FileList object & names is an Array of strings (file names)

$( "#buttonid_1" ).click(function(){
    var files = $('#fieldid_1').prop("files");
    var names = $.map(files, function(val){
        return val.name;
    });
    console.log(names[0]);
})

Reference: https://stackoverflow.com/a/10703223/3119231

Example: http://jsfiddle.net/ndfn782o/1/

EDIT: tested && 110% working

4 Comments

It is giving me following error: Uncaught TypeError: Cannot read property 'length' of undefined
1. Did you changed the id of save_1 ? 2. Are you sure the error appears here? :D
u are getting it on change event of a file but i want it on a button click.
In jsfiddle it is working but in my browser it is giving error message in the console uncaught TypeError: Cannot read property 'length' of undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.