admin管理员组

文章数量:1415654

I am trying to make an asynchronous file upload form with progress in percent. I thought this might work with a FormData object but I don't think the post is working. nothing happens when I submit. I have checked and there are no bugs and it works without the javascript,so the PHP is OK is there something fundamentally wrong with the code?

  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 request = new XMLHttpRequest();
      request.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) + '%'));
   }
});

           request.upload.addEventListener('load',function(event){
           document.getElementById('upload_progress').style.display = 'none';
});
           request.upload.addEventListener('error',function(event){
           alert("failed");

});
           request.open('POST','upload.php');
           request.setRequestHeader('Cache-Control','no-cache');
           document.getElementById('upload_progress').style.display = 'block';
};

           window.addEventListener('load',function(event){
           var submit = document.getElementById('submit');
           submit.addEventListener('click',handleUpload);
});

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 upload.php

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

I am trying to make an asynchronous file upload form with progress in percent. I thought this might work with a FormData object but I don't think the post is working. nothing happens when I submit. I have checked and there are no bugs and it works without the javascript,so the PHP is OK is there something fundamentally wrong with the code?

  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 request = new XMLHttpRequest();
      request.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) + '%'));
   }
});

           request.upload.addEventListener('load',function(event){
           document.getElementById('upload_progress').style.display = 'none';
});
           request.upload.addEventListener('error',function(event){
           alert("failed");

});
           request.open('POST','upload.php');
           request.setRequestHeader('Cache-Control','no-cache');
           document.getElementById('upload_progress').style.display = 'block';
};

           window.addEventListener('load',function(event){
           var submit = document.getElementById('submit');
           submit.addEventListener('click',handleUpload);
});

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 upload.php

if(!empty($_FILES['file'])){
    foreach  ($_FILES['file']['name'] as $key => $name) {
    move_uploaded_file($_FILES['file']['tmp_name'][$key],"myfiles/$name");
  }
}
Share Improve this question asked Feb 2, 2013 at 21:35 user2014429user2014429 2,58710 gold badges38 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

you have forgotten the most important line in your javascript code:

request.send(data);

after this:

request.setRequestHeader('Cache-Control','no-cache');

After the

request.setRequestHeader('Cache-Control','no-cache');

You forget to send the data..

request.send(data);

BTW, you need to send the proper headers

request.setRequestHeader("Content-type", ,,,
request.setRequestHeader("Content-length",...
request.setRequestHeader("Connection", "close");

本文标签: phpuploading multiple files with XMLHttprequestStack Overflow