Home » Javascript » Javascript Filereader sort by name

Javascript Filereader sort by name

Posted by: admin August 19, 2018 Leave a comment

Questions:

i’m trying to list my image preuploader sort by name but got some issues.i have dozen image with name img001 – img100 but when i input that the result show the files sort by image size. here is my code

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <title>generate nama ikan</title>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            <div class="container">
              <div class="row">
                <div class="col-md-12">
                  <h1>Nama Ikan</h1>
                  <input id="ingambar" type="file" multiple />
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <div class="content">
                      <br />
                        <table class="table">
                          <thead>
                            <th>gambar</th><th>nama</th>
                          </thead>
                          <tbody></tbody>
                        </table>
                  </div>
                </div>
              </div>
            </div>
        </div>
    </body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script>
    $('#ingambar').change(function () {
        for (var i=0, len = this.files.length; i < len; i++) {
            (function (j, self) {
                var reader = new FileReader()
                reader.onload = function (e) {
                    $('tbody').append('<tr><td><img width="100px" src="' + e.target.result + '"></td><input class="form-control" type="text" value="' + self.files[j].name.slice(0, -4) + '" name="namaikan[]" disabled/></td></tr>')
                }
                reader.readAsDataURL(self.files[j])
            })(i, this);
        }
    });
    </script>
</html>
Answers: