2015-08-23 2 views
0

У меня есть форма для загрузки нескольких файлов изображений в сочетании с Laravel 4.2 и Ajax. Но при загрузке изображения входной файл всегда равен NULL. Как я могу это исправить?Laravel 4.2 Входной файл Null

Это моя форма загрузки.

{{ Form::open(array('url' => route('member.font.upload.images'), 'files'=>true, 'id'=>'upload-images', 'method'=> 'post')) }} 
    {{ Form::file('image[]', array('multiple'=>true, 'id'=>'images')) }} 
    <input type="hidden" name="font" value="{{ $font->id }}"> 
{{ Form::close() }} 

И процесс загрузки в контроллер.

public function uploadImages() 
{ 
$rules = array(
    'font' => 'required' 
); 

$validator = Validator::make(Input::all(), $rules); 

if ($validator->fails()) { 
    return Response::json(['success' => false, 'message' => 'Failed upload image']); 
} else { 
    if(Input::hasFile('image')){ 
     $images = Input::file('image'); 
     foreach($images as $image) { 
      $imagename = time() . $image->getClientOriginalName(); 
      $upload = $image->move('images/', $imagename); 

      if($upload){ 
       $data = new Image; 
       $data->font_id  = Input::get('font'); 
       $data->name   = $imagename; 
       $data->save(); 
      } 
     } 
     return Response::json(['success' => true, 'message' => 'Success']); 
    }else{ 
     return Response::json(['success' => false, 'message' => 'File not found']); 
    } 
} 
} 

А затем процесс Ajax.

$('#images').change(function(e) { 
e.preventDefault(); 
$.ajax({ 
    url: $('#upload-images').attr('action'), 
    type: 'post', 
    dataType: 'json', 
    data: $('#upload-images').serialize(), 
    beforeSubmit: function() { 
     $('.add-images').removeClass('enabled').addClass('uploading'); 
    }, 
    success: function(data) { 
     $('.add-images').removeClass('uploading').addClass('enabled'); 
    }, 
    error: function() {} 
    }); 
}); 
+0

Использование 'serialize()' не загружает файлы. Нетрудно исследовать, как загружать файлы с помощью ajax – charlietfl

ответ

0

Изменить этот раздел:

{{ Form::open(array('url' => route('member.font.upload.images'), 'files'=>true, 'id'=>'upload-images', 'method'=> 'post')) }} 

To:

{{ Form::open(array('route' => 'member.font.upload.images', 'files'=>true, 'id'=>'upload-images', 'method'=> 'post')) }} 
+0

Спасибо, но все равно не работает ... – user3089464

+0

Можете ли вы показать мне свои маршруты.php? – Grald

+0

Этот мой маршрут Route :: post ('upload-images', array ('as' => 'member.font.upload.images', 'uses' => 'MemberController @ uploadImages')); – user3089464

0

Вам может понадобиться определить тип кодирования формы.

{{ Form::open(array('url' => route('member.font.upload.images'), 'files'=>true, 'id'=>'upload-images', 'method'=> 'post', 'enctype' => "multipart/form-data")) }} 

Для получения более подробной информации вы можете использовать этот W3Schools doc.

Смежные вопросы