2

below is my simple code using HTML 5 multiple file upload. All i need is to grab all the file names not the paths below is simple javascript code

  function getFileNames()
        {
            var path = $('#files').val();
            console.log(' path = ' + path);
        }

and html form is as

<form action='server.php' method='post' enctype='multipart/form-data' target="iframe">
                <input id="files" name='files[]' type='file' multiple>
                <input type='button' onclick="getFileNames()">
</form>

when i press the button the console output is as

path = Chrysanthemum.jpg

which is the first name of the file and i want the remaining names , any suggestions , comments are appreciated. thanks.

1
  • well after lot of work i found a nice solution.. check my answer. Commented Jan 24, 2012 at 7:15

3 Answers 3

6

well so here i am with the solution after lot of research, in case of input type file the value is stored in array as files with key name.

var files = $('input#files')[0].files;
var names = "";
$.each(files,function(i, file){
    names += file.name + " ";
});
alert(names);

fiddle : http://jsfiddle.net/raj_er04/nze2B/1/

pure javascript

function getFileNames(){
    var files = document.getElementById("files").files;
    var names = "";
    for(var i = 0; i < files.length; i++)
        names += files[i].name + " ";
    alert(names);
}

fiddle : http://jsfiddle.net/raj_er04/nze2B/2/

Sign up to request clarification or add additional context in comments.

Comments

1
    function getFileNames()
    {
       var files = document.getElementById("files").files;         
    for (var i = 0, f; f = files[i]; i++) {
          console.log(' name= ' + f.name);
alert(' path = ' + f.name);
        }
    }

2 Comments

i got the files = document.getElementById('files'); and remaining is your code doesn't do any thing no errors no log as well.
Here is the entire code with console log aswell as alert function getFileNames() { var files = document.getElementById("files").files; for (var i = 0, f; f = files[i]; i++) { console.log(' path = ' + f.name); alert(' path = ' + f.name); } }
-1

Instead of

var path = $('#files').val();

loop through the array, use

var names = "";
var path = $('#files').each(function(i, el) {
    names = names + el.val() + ",";
});

4 Comments

Thanks but i am getting this error in firefox. el.val is not a function [Break On This Error] names = names + el.val() + ",";
Use $(el).val() instead of el.val()
it only returns one file name though
What "array"? #files is an id selector, there is only going to be a single element in the jQuery object

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.