2

html Markup:

<form action="" method="post" enctype="multipart/form-data">
    <input id="fileSelect" type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
    <input type="submit" value="Upload!" onclick='showFileModified();' />
</form>

Script:

<script type='text/javascript'>
function showFileModified()
{
    var input, file;

    input = document.getElementById( 'fileSelect' );

        file = input.files[0];

        file.lastModifiedDate;
        alert(file.lastModifiedDate);
}
</script>

If i select more than one file lets say 4 files. i want each file modified date alert(file.lastModifiedDate). how can i use array here to achieve this.

what i tried:

function showFileModified()
{
        var input[], file;

        input = document.getElementById( 'fileSelect' );

        for(var i=0; i<=input.length; i++)
        {
            alert('d');
            file = input.files[i];

            file.lastModifiedDate;
            alert(file.lastModifiedDate);

       }
}
7
  • 1
    input.files is your array. shouldn't you check it's length rather than input? Commented Jan 13, 2015 at 15:58
  • if i select 3 files from input, isn't it save it in input rather than file? Commented Jan 13, 2015 at 16:04
  • This line: for(var i=0; i<=input.length; i++) input.length makes no sense. You only have one input, which you selected by id, it doesn't have a length. Commented Jan 13, 2015 at 16:05
  • what should i do then? how do i make the for loop right? Commented Jan 13, 2015 at 16:07
  • uhmmm.... use the length of the input.files array instead? You know, the array you're iterating over? Commented Jan 13, 2015 at 16:07

1 Answer 1

2

You may use this to get all modified date values:

function showFileModified(){
    var input = document.getElementById( 'fileSelect' );
    for (var i = 0; i < input.files.length; ++i){
        alert(input.files[i].lastModifiedDate);
    }      
}

input.files is an array with all selected files listed. That's why you need to loop through this array (which is achieved by looping from 0 to array.length - 1)

But you should take care using multiple ids for a single tag as well as you should not use names with special characters:

<input id="fileSelect" type="file" id="file" name="files[]" multiple="multiple" accept="image/*"/>

Better use something like this (you would not even need to use a name:

<input id="fileSelect" type="file" name="fileSelector" multiple="multiple" accept="image/*" />
Sign up to request clarification or add additional context in comments.

Comments

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.