filereader result order changing while uploading multiple images in javascript

MMMMS

I am trying to upload multiple images and place in separate img tag using javascript.Here is what i tried,

MyJsp.jsp :

  <div>
  <img alt="Image1" id="Image1" src="" width="130px" height="90px"><br><br>
  <img alt="Image2" id="Image2" src="" width="130px" height="90px"><br><br>
  <img alt="Image3" id="Image3" src="" width="130px" height="90px"><br><br> 
  <img alt="Image4" id="Image4" src="" width="130px" height="90px"><br><br>
  <img alt="Image5" id="Image5" src="" width="130px" height="90px"><br><br>
  </div>

<input type="file" id="files" name="files[]" accept="image/gif,image/jpeg"/ multiple>
<input type="text" id="imginsert" value="1">

MyJS.js :

$(function(){
document.querySelector('#files').addEventListener('change', handleFileSelect, false);

function handleFileSelect(evt) {    

    var files = evt.target.files; // FileList object
   // alert("1");
   // Loop through the FileList and render image files as thumbnails.
   for (var i = 0, f; f = files[i]; i++) {

     // Only process image files.
     if (!f.type.match('image.*')) {
       continue;
     }

     var reader = new FileReader();   
     reader.onload = (function(theFile) {
       return function(e) {
           var count=$('#imginsert').val();
                if(count==1){

                $('#Image1').attr("src",e.target.result);
                $('#imginsert').val('2');

                //alert("first :"+e.target.result);
                }
                else if(count==2)
                {

                    //alert("else if 1");
                    $('#Image2').attr("src",e.target.result);
                    $('#imginsert').val('3');

                    //alert("2 :"+e.target.result);
                }
                else if(count==3)
                {

                    //alert("else if 2");
                    $('#Image3').prop("src",e.target.result);
                    $('#imginsert').val('4');

                    //alert("3 :"+e.target.result);
                }
                else if(count==4)
                {

                    $('#Image4').prop("src",e.target.result);
                    $('#imginsert').val('5');

                    //alert("4 :"+e.target.result);
                }
                else if(count==5)
                {

                    $('#Image5').prop("src",e.target.result);
                    $('#imginsert').val('6');

                    //alert("5 :"+e.target.result);
                }
                else
                {
                    alert("You can upload only 5 images.");
                }

       };

     })(f); 

     // Read in the image file as a data URL.
     reader.readAsDataURL(f);
   }

 }

});

If i uploading Img1.jpg,Img2.jpg,Img3.jpg,Img4.jpg,Img5.jpg means the OP is :

Img2.jpg
Img4.jpg
Img1.jpg
Img3.jpg
Img5.jpg

My Expecting OP is :

Img1.jpg
Img2.jpg
Img3.jpg
Img4.jpg
Img5.jpg

where i am doing mistake to place orderly like uploading images order?

Musa

readAsDataURL is asynchronous so you can't guarantee that they will finish in any order. Just pass the index of the file to your IIFE with your file

 reader.onload = (function(theFile, count) {
   return function(e) {
            if (count > 5)
            {
                alert("You can upload only 5 images.");
            }
            else{
                $('#Image'+count).prop("src",e.target.result);
            }

   };

 })(f,i+1); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript FileReader Multiple Images

From Dev

javascript FileReader: preview for images in multiple files batches

From Dev

Changing src on multiple images - Javascript

From Dev

Jquery : Post Request is breaking while uploading multiple images

From Dev

Uploading multiple images with volley?

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images with paperclip?

From Dev

Uploading multiple images with Django

From Dev

Uploading multiple images in codeigniter?

From Dev

Django multiple images not uploading

From Dev

Converting result of FileReader to javascript objects

From Dev

Uploading Multiple Images using CarrierWave

From Dev

Multiple images rename & uploading in PHP

From Dev

Multiple images rename & uploading in PHP

From Dev

Changing multiple images with JQuery

From Dev

All images not uploading in multiple images upload

From Dev

Dynamically uploading images in HTML and Javascript

From Dev

I am getting empty array, I need array of image while uploading multiple images in Codeigniter

From Dev

Preloading and changing images with JavaScript

From Dev

Changing images with mouseover in Javascript

From Java

JavaScript: Why `FileReader.readAsBinaryString()` corrupts result

From Dev

Trouble uploading binary files using JavaScript FileReader API

From Dev

Errors coming while uploading images to FTP

From Dev

Error while uploading images through SOAP in magento

From Dev

changing multiple images using onclick

From Dev

Uploading multiple images from Share Extension

From Dev

Uploading multiple images with other parameters in Swift

From Dev

Django REST: Uploading and serializing multiple images

From Dev

Uploading multiple images to mysql database on Apache server

Related Related

  1. 1

    Javascript FileReader Multiple Images

  2. 2

    javascript FileReader: preview for images in multiple files batches

  3. 3

    Changing src on multiple images - Javascript

  4. 4

    Jquery : Post Request is breaking while uploading multiple images

  5. 5

    Uploading multiple images with volley?

  6. 6

    Uploading multiple images in codeigniter?

  7. 7

    Uploading multiple images with paperclip?

  8. 8

    Uploading multiple images with Django

  9. 9

    Uploading multiple images in codeigniter?

  10. 10

    Django multiple images not uploading

  11. 11

    Converting result of FileReader to javascript objects

  12. 12

    Uploading Multiple Images using CarrierWave

  13. 13

    Multiple images rename & uploading in PHP

  14. 14

    Multiple images rename & uploading in PHP

  15. 15

    Changing multiple images with JQuery

  16. 16

    All images not uploading in multiple images upload

  17. 17

    Dynamically uploading images in HTML and Javascript

  18. 18

    I am getting empty array, I need array of image while uploading multiple images in Codeigniter

  19. 19

    Preloading and changing images with JavaScript

  20. 20

    Changing images with mouseover in Javascript

  21. 21

    JavaScript: Why `FileReader.readAsBinaryString()` corrupts result

  22. 22

    Trouble uploading binary files using JavaScript FileReader API

  23. 23

    Errors coming while uploading images to FTP

  24. 24

    Error while uploading images through SOAP in magento

  25. 25

    changing multiple images using onclick

  26. 26

    Uploading multiple images from Share Extension

  27. 27

    Uploading multiple images with other parameters in Swift

  28. 28

    Django REST: Uploading and serializing multiple images

  29. 29

    Uploading multiple images to mysql database on Apache server

HotTag

Archive