douyan6871 2017-09-04 11:38
浏览 111
已采纳

如何在多次上载后使用XMLHttpRequest显示确认消息

I have a system that enables an upload of many images with XMLHttpRequest that work perfectly only that at the end it does not display a confirmation message even though i have place the confirmation message in the program.

Here is the Javascript :

    <script>

var handleUpload = function(event){
      event.preventDefault();
      event.stopPropagation();

  var fileInput = document.getElementById('file');
  var data = new FormData();

  for(var i = 0; i < fileInput.files.length; ++i)
  {
     data.append('file[]',fileInput.files[i]);
  }


  var xhr = new XMLHttpRequest();
      xhr.upload.addEventListener('progress',function(event){

       if(event.lengthComputable)
       {

          var percent = event.loaded / event.total;
          var progress = document.getElementById('upload_progress');

                while (progress.hasChildNones())
                {
                       progress.removeChild(progress.firstChild);
                }
               progress.appendChild(document.createTextNode(Math.round(percent * 100) + '%'));
       }

});

            xhr.upload.addEventListener('load',function(event)
            {
            document.getElementById('upload_progress').style.display = 'none';
            });

            xhr.upload.addEventListener('error',function(event)
            {

            alert("failed");

            });


           xhr.open('POST','upload.php');
           xhr.setRequestHeader('Cache-Control','no-cache');
           xhr.send(data);

           document.getElementById('upload_progress').style.display = 'block';

            document.getElementById("upload_progress").innerHTML=xhr.responseText;


};

           window.addEventListener('load',function(event){



           var submit = document.getElementById('submit');
           submit.addEventListener('click',handleUpload);


});



</script>

And the rest of the HTML

<div id="upload_progress"></div>


  

<form id="form" action="" method="post" enctype="multipart/form-data">
    <input id="file" name="file[]" type="file" multiple /><br>
    <input type="submit" name="send" id ="submit" value="send">
</form>

And finally my php code that enables the upload from upload.php

<?php

if(!empty($_FILES['file']))
{
############################################################### 
    foreach  ($_FILES['file']['name'] as $key => $name) 
    {
    move_uploaded_file($_FILES['file']['tmp_name'][$key],"myfiles/$name");


    }
############################################################### 
echo "God is good";

}

?>

Everything works perfectly, the files are uploaded However the confirmation message God is good does not display.

How to display a confirmation message with XMLHttpRequest after a multiple Upload ?

  • 写回答

2条回答 默认 最新

  • dongshou1991 2017-09-04 18:17
    关注

    First of all it is not displaying because of document.getElementById('upload_progress').style.display = 'none';

    That line of code is setting the style as not be displayed so no matter what you do, it is not going to display except you either change it to document.getElementById('upload_progress').style.display = 'unset'; or you comment that line out and then move the updater into an uploader function for example you entire ajax code might look like

    <script>
    
    var handleUpload = function(event){
          event.preventDefault();
          event.stopPropagation();
    
      var fileInput = document.getElementById('file');
      var data = new FormData();
    
      for(var i = 0; i < fileInput.files.length; ++i)
      {
         data.append('file[]',fileInput.files[i]);
      }
    
    
      var xhr = new XMLHttpRequest();
    
          xhr.upload.addEventListener('progress',function(event){
    
           if(event.lengthComputable)
           {
    
              var percent = event.loaded / event.total;
              var progress = document.getElementById('upload_progress');
    
                    while (progress.hasChildNones())
                    {
                           progress.removeChild(progress.firstChild);
                    }
    
                   progress.appendChild(document.createTextNode(Math.round(percent * 100) + '%'));
           }
    
    });
    
                xhr.upload.addEventListener('load',function(event)
                {
                /*
    We comment this line out so as to enable the display of your information from your php file
                document.getElementById('upload_progress').style.display = 'none';
                */
                });
    
                xhr.upload.addEventListener('error',function(event)
                {
    
                alert("failed");
    
                });
    
    
               xhr.open('POST','upload.php');
               xhr.setRequestHeader('Cache-Control','no-cache');
    
    
    
               /*
    
               */
    
               xhr.upload.onprogress = function(e) 
              {
                if (e.lengthComputable) 
                {
                  var percentComplete = (e.loaded / e.total) * 100;
    
                  console.log(percentComplete + '% uploaded');
    
                  document.getElementById("upload_progress").innerHTML="<img src='images/facebook_style_loader.gif'>" + percentComplete + '% uploaded'; 
    
    
    
                }
              };
    
    
               xhr.onload = function() 
              {
                    if (this.status == 200) 
                    {
    
    
                            if (document.getElementById)
                            {   
    
                                            document.getElementById("upload_progress").innerHTML=xhr.responseText;
    
    
                            }
    
    
    
                    }
              };
    
    
    
               /*
    
               */
    
    
    
    
               xhr.send(data);
    
              /*
               document.getElementById('upload_progress').style.display = 'block';
            */
    
    
    
    };
    
               window.addEventListener('load',function(event){
    
    
    
               var submit = document.getElementById('submit');
               submit.addEventListener('click',handleUpload);
    
    
    
    
    
                });
    
    
    
    </script>
    

    I have added and Upload handler and Onload handler to make things easier for you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!