使用Ajax JQuery和PHP的进度条将.csv文件导入MySQL

时间:2016-10-28 22:50:56

标签: php jquery mysql ajax import-csv

我尝试使用PHP,JQuery和HTML为进度条编写代码。 所以,我为一个PHP文件做了一个Ajax请求,在success Ajax参数中,我搜索了从PHP文件中请求的data,就像这样..

success: function(data){       
    if(data == 'Error1'){
        alert("The File is not a .csv");
    }else if(data == "Error2"){
        alert("Please select a file to import!");
    }else{  
        $('#consumidor_table').html(data);
        alert("The importation has been made!");
    }
}

else确实打印了带有MySql DB行的表。 PHP文件从HTML输入读取.csv并在DB中插入这些行。

实际上,我做进度条的代码是: 成功参数之前

xhr: function(){
 var xhr = new window.XMLHttpRequest();                      
 xhr.upload.addEventListener("progress", function(evt){
    if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        console.log(evt.loaded);
        console.log(evt.total); 
        console.log(percentComplete*100);
        addProgress(percentComplete*100);
    }
    });
    return xhr;
 },

整个代码: JQuery; PHP URL

错误:当上传文件并提交表单时,console.log(evt.total);console.log(evt.loaded);会打印相同的值,并且进度条已完全显示,但是Ajax继续请求PHP文件,我的表还是空的。

那么我可以对我的进度条做些什么来处理来自PHP文件的响应?

1 个答案:

答案 0 :(得分:0)

我不确定加载,即使我正在搜索显示完整程度的内容。但我有一个工作代码将CSV上传到Mysql。

<?php  
 if(!empty($_FILES["employee_file"]["name"]))  
 {  
      $connect = mysqli_connect("123.123.123.99","db_username","db_password","db_Name") or die('Unable To Connect'); 
      $output = '';  
      $allowed_ext = array("csv");  
      $extension = end(explode(".", $_FILES["employee_file"]["name"]));  
      if(in_array($extension, $allowed_ext))  
      {  
           $file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');  
           fgetcsv($file_data);  
           while($row = fgetcsv($file_data))  
           {  
                $customerDetails = mysqli_real_escape_string($connect, $row[0]);  
                $cod = mysqli_real_escape_string($connect, $row[1]);  
                $trackingID = mysqli_real_escape_string($connect, $row[2]);  
                $shipmentType = mysqli_real_escape_string($connect, $row[3]);  
                $vendorName = 'eKart';  
                $query = "  
                INSERT INTO nss_delivery  
                     (customerDetails, cod, trackingID, shipmentType, vendorName)  
                     VALUES ('$customerDetails','$cod', '$trackingID', '$shipmentType', '$vendorName')  
                ";  
                mysqli_query($connect, $query);  
           }  
           $select = "SELECT * FROM nss_delivery ORDER BY deliveryID DESC";  
           $result = mysqli_query($connect, $select);  
           $output .= '  
                <table class="table table-bordered">  
                     <tr>  
                           <th width="5%">ID</th>  
                           <th width="25%">Customer Details</th>  
                           <th width="35%">COD Amount</th>  
                           <th width="10%">Tracking ID</th>  
                           <th width="20%">ShipmentType</th>  
                           <th width="5%">Vendor Name</th> 
                     </tr>  
           ';  
           while($row = mysqli_fetch_array($result))  
           {  
                $output .= '  
                     <tr>  
                          <td>'.$row["deliveryID"].'</td>  
                          <td>'.$row["customerDetails"].'</td>  
                          <td>'.$row["cod"].'</td>  
                          <td>'.$row["trackingID"].'</td>  
                          <td>'.$row["shipmentType"].'</td>  
                          <td>'.$row["vendorName"].'</td>  
                     </tr>  
                ';  
           }  
           $output .= '</table>';  
           echo $output;  
      }  
      else  
      {  
           echo 'Error1';  
      }  
 }  
 else  
 {  
      echo "Error2";  
 }  
 ?>  

希望这能以某种方式帮助某人:)

谢谢!如果您找到此上传的进度条的解决方案,请告诉我。