Show a progress on multiple file upload Jquery/Ajax

后端 未结 2 2010
忘掉有多难
忘掉有多难 2020-12-07 14:32

I have upload form that allows users to upload multiple files. I decided that a progress bar would be good if the files are quite large. Below is my source code. I am new to

2条回答
  •  伪装坚强ぢ
    2020-12-07 15:36

    This is your HTML form

    This is your JQuery and ajax. By default the fileInput is hidden.

    Upload Button clicked

    $("#btnupload").click(function(e) {
    
        $("#fileupload").click();
        e.preventDefault();
    
    });
    
    
    $('#fileupload').change(function (e) {
    
        $("#imageuploadform").submit();
        e.preventDefault();
    
    });
    
    $('#imageuploadform').submit(function(e) {
    
        var formData = new FormData(this);
    
        $.ajax({
            type:'POST',
            url: 'ajax/uploadImages',
            data:formData,
            xhr: function() {
                    var myXhr = $.ajaxSettings.xhr();
                    if(myXhr.upload){
                        myXhr.upload.addEventListener('progress',progress, false);
                    }
                    return myXhr;
            },
            cache:false,
            contentType: false,
            processData: false,
    
            success:function(data){
                console.log(data);
    
              alert('data returned successfully');
    
            },
    
            error: function(data){
                console.log(data);
            }
        });
    
        e.preventDefault();
    
    });
    
    
    function progress(e){
    
        if(e.lengthComputable){
            var max = e.total;
            var current = e.loaded;
    
            var Percentage = (current * 100)/max;
            console.log(Percentage);
    
    
            if(Percentage >= 100)
            {
               // process completed  
            }
        }  
     }
    

    Your php code looks like this

    $path);
    
                                      $filename = "image_" . $date . "_" .$uid . "." . $ext;
                                      $this->createthumb($i,$filename);
    
                                        // move uploaded file from temp to uploads directory
                                      if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $path))
                                      {
                                        //$status = 'Image successfully uploaded!';
                                          //perform sql updates here
    
                                      }
                                      else {
                                        $status = 'Upload Fail: Unknown error occurred!';
                                      }
    
    
                              }
                              else {
                                $status = 'Upload Fail: Unsupported file format or It is too large to upload!';
                              }
                        }
                        else {
                          $status = 'Upload Fail: File not uploaded!';
                        }
                    }
                  }
                else {
                  $status = 'Bad request!';
                }
    
    
                echo json_encode($json);
    
    ?>
    

提交回复
热议问题