使用ajax和dropzone的多个上传文件

时间:2018-08-05 15:32:01

标签: ajax dropzone

我想使用ajax和dropzone发送多个文件。

这是我的html

<form action="#" class="dropzone" id="ls">
  <div class="fallback">
    <input name="images" id="images" type="file" multiple />
  </div>
  <button  type="submit" name="submit" id="startUpload">UPLOAD</button>
</form>

那是js代码

Dropzone.autoDiscover = false;

var myDropzone = new Dropzone('#ls', {       
    paramName: "names", 
    maxFilesize: 3.0, 
    maxFiles: 4,
    parallelUploads: 10000,
    uploadMultiple: true, 
    autoProcessQueue: true
});

$('#startUpload').on('click', function () {
    myDropzone.processQueue();
});

$(document).ready(function(){
    $('#ls').ajaxForm({

        beforeSubmit:function(){
            $('#startUpload').html("<img src='tools/img/loading.svg' style='width:100px; height:25px;' />");
        },
       url: "includes/profile_includes/func_infos.php",
         type: "post",
        success:function(data){
          alert(data);
            $('#startUpload').html('');
        },
        error:function(){
            $('#startUpload').html('Images upload failed, please try again.');
        }
    });
});

那是php代码;

    if(isset($_POST['submit'])){
// File upload configuration
  require("../../includes/common_includes/connect_db.php");  
$targetDir = '../../../members/'.infos_profile()->id.'/photos_visited_lived_places/';
$allowTypes = array('jpg','png','jpeg','gif');

$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
    $image_name = $_FILES['images']['name'][$key];
    $tmp_name   = $_FILES['images']['tmp_name'][$key];
    $size       = $_FILES['images']['size'][$key];
    $type       = $_FILES['images']['type'][$key];
    $error      = $_FILES['images']['error'][$key];

    // File upload path
    $fileName = basename($_FILES['images']['name'][$key]);
    $targetFilePath = $targetDir . $fileName;

    // Check whether file type is valid
    $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
    if(in_array($fileType, $allowTypes)){    
        // Store images on the server
        if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$targetFilePath)){
            $images_arr[] = $targetFilePath;
            echo "success";
        }else{
          echo "echec";
        }
    }
}

}

这是错误:

注意:未定义索引:C:\ wamp64 \ www .....中的图像

提示:警告:为C:\ wamp64 \ www ....中的foreach()提供了无效的参数。

有人可以帮助我吗?

0 个答案:

没有答案