在javascript中上载多个图像时filereader结果顺序发生更改

在javascript中上载多个图像时filereader结果顺序发生更改,javascript,jquery,javascript-events,filereader,Javascript,Jquery,Javascript Events,Filereader,我正在尝试上传多个图像,并使用javascript将其放置在单独的img标记中 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&g

我正在尝试上传多个图像,并使用javascript将其放置在单独的img标记中

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">
如果我上传Img1.jpg、Img2.jpg、Img3.jpg、Img4.jpg、Img5.jpg表示OP是:

Img2.jpg
Img4.jpg
Img1.jpg
Img3.jpg
Img5.jpg
我的期望是:

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

我在哪里犯了像上传图像这样的顺序摆放错误?

readAsDataURL
是异步的,所以你不能保证它们会以任何顺序完成。只要把文件的索引和你的文件一起传给你的生活

 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); 

为什么你后端的订单是必需的?@Nano抱歉我找不到你!?如果你上传多张图片到你的后端,如果你想用它们做不同的事情,你通常不会混合图片(如果这就是为什么顺序很重要的原因)。@Nano是的,我正在尝试匹配以下问题(图片到文本),所以我想从上传图片开始按顺序放置和显示,谢谢你宝贵的回答。它像我期望的那样工作良好。
 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);