laravel 5.1 formData - AJAX - PHP-上传多个文件

时间:2015-07-23 02:34:46

标签: ajax laravel uploading

我无法从文件上传中获取formData

form.blade.php

<form method="POST" class="form-horizontal" enctype="multipart/form-data" id="modal-file-upload">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="hidden" name="folder" value="" id="modal-file-upload-folder">
    <div class="modal-header">
        <h4 class="modal-title">Upload New File</h4>
    </div>
    <div class="modal-body">
        <div class="form-group">
            <label for="file" class="col-sm-3 control-label">
                File
            </label>
            <div class="col-sm-8">
                <input multiple="true" accept="image/*" required="true" id="fileToUpload" type="file" name="file[]">
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" onclick="close_upload_view()">Cancel</button>
        <button class="btn btn-primary" id="btn-upload">Upload File</button>
    </div>
</form>

控制器

public function uploadMultiFile(Request $request) {
    // getting all of the post data
    $data = $request->input('formData');
    var_dump($data);  current   return null
}

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
<script>
    // Confirm file delete
	function close_upload_view() {
      $("#modal-file-upload").modal("hide");
	  return false;
    }
	$.ajaxSetup({
	   headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
	});
	
    $("#modal-file-upload").submit(function(e){
		e.preventDefault();
		//var formData = new FormData(document.getElementById('modal-file-upload')[0]);
		
		 var fd = new FormData();
		
		 var ins=document.getElementById('fileToUpload').files.length;

		  for(var x=0;x<ins;x++)
		  {
			   fd.append("fileToUpload[]", document.getElementById('fileToUpload').files[x]);
		  }
		
		
		
		var folder = $("input#modal-file-upload-folder").val();
		var token =  $("input[name=_token]").val();
		var dataString = 'formData='+fd+'&folder='+folder+'&token='+token; 
		
		
		$.ajax({
			type: "POST",
			url: '{!! url() !!}/admin/upload/multifile',
			cache: false,
			data : dataString,
			processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server it
			dataType: 'JSON',
			success : function(data){
				console.log(data);
			}
		},"json");

     });
    </script>
&#13;
&#13;
&#13;