当我输入$ request->文件('photo')或$ request->输入('photo')时,我得到null,即使我在FORM :: open中有files => true。我在login.blade.php中使用注册表单,它看起来像:
{{FORM::open(['id'=>'registerForm','url'=>'register','files'=>true,'novalidate'])}}
<h1>Krijo llogari</h1>
<div>
{{FORM::text('first_name',null,['class'=>'form-control','required','placeholder'=>'Emri'])}}
</div>
<div>
{{FORM::text('last_name',null,['class'=>'form-control','required','placeholder'=>'Mbiemri'])}}
</div>
<div>
{{FORM::password('password',['class'=>'form-control','placeholder'=>'Fjalëkalimi','required'])}}
</div>
<div>
{{FORM::password('password_confirmation',['class'=>'form-control','placeholder'=>'Konfirmo fjalëkalimin','required'])}}
</div>
<div>
{{FORM::select('acedemical_title_id',array_merge(['0'=>'Zgjedhe titullin akademik'],$academicalTitles),null,['class'=>'form-control','required','style'=>'border-radius:2px'])}}
</div>
<br>
<div>
{{FORM::select('cpa_id',array_merge(['0'=>'Zgjedhe profesor/asistent'],$cpas),null,['class'=>'form-control','required','style'=>'border-radius:2px'])}}
</div>
<br>
<div>
{{FORM::text('personal_number',null,['class'=>'form-control','required','placeholder'=>'Numri personal','maxlength'=>'10'])}}
</div>
<div>
{{FORM::email('email',null,['class'=>'form-control','required','placeholder'=>'E-mail'])}}
</div>
<div>
<div class="input-group image-preview">
<span class="input-group-btn">
<!-- image-preview-clear button -->
<button type="button" class="btn btn-default image-preview-clear" style="display:none;">
<span class="glyphicon glyphicon-remove"></span> Pastro
</button>
<!-- image-preview-input -->
<div class="btn btn-default image-preview-input">
<span class="glyphicon glyphicon-folder-open"></span>
<span class="image-preview-input-title">Zgjedh foton</span>
{!! FORM::file('photo') !!}
</div>
</span>
</div>
</div>
<br>
<div>
{{FORM::submit('Regjistrohu',['class'=>'btn btn-success pull-right'])}}
</div>
<div class="clearfix"></div>
<div class="separator">
<p class="change_link">Keni llogari?
<a href="#signin" class="to_register"> Kyçu </a>
</p>
<div class="clearfix"></div>
<br />
<div>
<h1><i class="fa fa-xalfa"></i> XALFA - Për krijimin e orarit!</h1>
<p>©2016 All Rights Reserved. XALFA Inc. Privacy and Terms</p>
</div>
</div>
{{FORM::close()}}
虽然我的控制器看起来像这样:
public function store(Request $request)
{
try{
//dd($request->file('photo'));
$destination_folder = 'Uploads/';
$validator = Validator::make($request->all(),[
'first_name' => 'bail|required|alpha|max:190',
'last_name' => 'bail|required|alpha|max:190',
'email' => 'bail|required|email|max:190',
'password'=>'bail|required|confirmed|max:190',
'personal_number'=>'bail|required|numeric',
'cpa_id' => 'bail|required|numeric',
'acedemical_title_id' => 'bail|required|numeric',
'photo' => 'bail|require|image|mime:jpeg,png|max:1000000',
]);
if($validator->fails()){
return response()->json([
'fails'=>true,
'title'=>'Gabim gjatë validimit!',
'msg'=>$validator->getMessageBag()->toArray()
],400);
}
$data = $request->all();
if($request->hasFile('photo')){
dd('asdasgasdasd');
$file = $request->file('photo');
$filename = $file->getClientOriginalName().'-'.str_random(8);
$file->move($destination_folder,$filename);
$data['photo']=$destination_folder.$filename;
}else{
dd('nuk hini hiq');
}
if($user = Sentinel::register($data)){
if($activation = Activation::create($user)){
if($role = Sentinel::findRoleBySlug('user')){
$role->users()->attach($user);
$this->sendMail($user, $activation->code);
return response()->json([
'success'=>true,
'title'=>'Aktivizimi',
'msg'=>'Për të aktivizuar llogarinë tuaj kontrollo postën tuaj elektronike!'
],200);
}else{
return response()->json([
'fails'=>true,
'title'=>'Gabim internal',
'msg'=>'Ju lutemi kontaktoni mbështetësit e faqes!'
],400);
}
}
}else{
return response()->json([
'fails'=>true,
'title'=>'Gabim gjatë regjistrimit',
'msg'=>'Ju lutemi shikoni për parregullsi në të dhëna!'
],400);
}
}catch(QueryException $e){
return response()->json([
'fails'=>true,
'title'=>'Gabim në server!',
'msg'=>'Ju lutem kontaktoni mirëmbajtësit e faqes.',
],500);
}
catch(ErrorException $e){
return response()->json([
'fails'=>true,
'title'=>'Gabim në server!',
'msg'=>'Ju lutem kontaktoni mirëmbajtësit e faqes.'
],500);
}
}
我的ajax javascript代码如下:
$('#registerForm').submit(function(e){
e.preventDefault();
$.ajaxSetup({
headers:{
'X-CSRF-TOKEN': $('meta[name=csrf-token]').attr('content')
}
});
var form = $(this), formData=form.serialize(), url=form.attr('action');
$.ajax({
type: 'POST',
url: url,
data: formData,
dataType: 'JSON',
statusCode: {
500: function(data){
BootstrapDialog.show({
title: data.responseJSON['title'],
message: data.responseJSON['msg'],
buttons: [{
label: 'Close',
action: function(dialog) {
dialog.close();
}
}]
});
},
400: function(data){
var msg="";
$.each(data.responseJSON['msg'], function(index, value){
$.each(this,function(i,v){
msg += v+'\n';
})
});
BootstrapDialog.show({
title: data.responseJSON['title'],
message: msg,
buttons: [{
label: 'Close',
action: function(dialog) {
dialog.close();
}
}]
});
},
200: function(data){
BootstrapDialog.show({
title: data.responseJSON['title'],
message: data.responseJSON['msg'],
buttons: [{
label: 'Close',
action: function(dialog) {
dialog.close();
}
}]
});
}
}
});
});
我的路线文件如下:
Route::post('register','UsersController@store');
答案 0 :(得分:0)
答案 1 :(得分:0)
要从ajax中获取input type = file数据而不是序列化表单,只需创建一个FormData的新对象,如下所示:
var data = new FormData(this)
这个&#39;是形式。谢谢你的回答