Наконец-то нашёл, как сделать правильную форму файла ajax
Вот как я это сделал
Просмотр
<form action="{{ route('Project/UploadDocumentProject') }}" method="post" id ="form0" enctype="multipart/form-data" >
{{ csrf_field() }}
<input id="ProjectID" name="ProjectID" type="hidden" value="{{$Project->id}}">
<div class="form-group">
<input name="fileAjax" id="fileAjax" class="form-control" type="file">test</textarea>
</div>
<div class="form-group">
<button id ="test" class="btn btn-success upload-image" type="submit" >Upload</button>
</div>
</form>
Запрос Ajax
var files;
$('input[type=file]').on('change', prepareUpload);
function prepareUpload(event)
{
files = event.target.files;
}
$("#form0").submit(function (e) {
e.preventDefault();
var form = $("#form0");
var data = new FormData();
$.each(files, function(key, value)
{
data.append(key, value);
});
var $ProjectID = $('#ProjectID').val();
data.append('ProjectID', $ProjectID);
$.ajax({
type: "post",
url: form.attr('action'),
data: data,
processData: false,
contentType: false,
cache: false,
enctype: 'multipart/form-data',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function (data) {
//some code here...
}
});
});
Контроллер
public function UploadDocumentProject (Request $Request) {
$path = $Request->file('0')->store('document');
$filename = $Request->file('0')->getClientOriginalName();
$Request->file('0')->storeAs('/public/projects/files',$filename);
//some code here...
return Response::json(array('var1'=>$val1 ,...));
}
Маршрут
Route::post('Project/UploadDocumentProject', ['as'=>'Project/UploadDocumentProject','uses'=>'ProjectController@UploadDocumentProject']);