2014-03-31 103 views
3

嗨,我試圖讓通過laravel的圖片上傳,一切工作正常,但現在我想改變上傳到jQuery的上傳,而不是後來我得到一個500 internal server errorLaravel 4圖像上傳

所以當我用Jquery處理事情失敗。任何人都知道問題可能是什麼? HTML:

{{ Form::open(array('url' => '../public/posts/add', 'class'=>'form-horizontal', 'role' => 'form', 'id' => 'addPin', 'files' => true)) }} 

      <div id="validation-errors" class="alert alert-danger" hidden> 
       <p>Some errors occured</p> 
       <ul></ul> 
      </div> 

      <!-- Image Type --> 
       <span id="type-image" class="type-media"> 
        <div class="form-group"> 
         <label class="col-sm-3 control-label">Title</label> 
         <div class="col-sm-9"> 
          {{ Form::text('Image-title', null, array('class' => 'form-control', 'placeholder' => '')) }} 
         </div> 
        </div> 

        <div class="form-group"> 
         <label class="col-sm-3 control-label">Choose file</label> 
         <div class="col-sm-9"> 
          {{ Form::file('Image-file') }} 
          <p class="help-block">Only .jpg, .png, .gif, .bmp allowed.</p> 
         </div> 
        </div> 

        <div class="form-group"> 
         <label class="col-sm-3 control-label">Description</label> 
         <div class="col-sm-9"> 
          {{ Form::textarea('Image-description', null, array('class' => 'form-control', 'rows' => '3')) }} 
         </div> 
        </div> 
       </span> 


      <div class="modal-footer"> 
       {{ Form::submit('Close', array('class' => 'btn btn-default', 'data-dismiss' => 'modal')) }} 
       {{ Form::submit('Pin it, babe!', array('class' => 'btn btn-info')) }} 
      </div> 
      {{ Form::close() }} 

jQuery的

addPin.on('submit', function() { 
    event.preventDefault(); 
    var errorForm = addPin.find('div#validation-errors'); 
    $.ajax({ 
     url: '../public/posts/add', 
     type: 'post', 
     cache: false, 
     data: addPin.serialize(), 
     beforeSend: function() { 
      errorForm.hide(); 
      errorForm.find("ul").empty(); 
     }, 
     success: function(data) { 
      if(data.success == false) { 
       var arr = data.errors; 
       console.log(arr); 
       $.each(arr, function(index, value){ 
        if (value.length != 0){ 
         errorForm.find("ul").append('<li>'+ value +'</li>'); 
        } 
       }); 
       errorForm.show(); 
       } else { 
       location.reload(); 
      } 
     }, 
     error: function() { 
      alert('Something went to wrong.Please Try again later...'); 
     } 
    }); 
    return false; 
}); 

PHP

public function postAdd(){ 
     if (Auth::check()){ 
        $rules = array(
         'Image-title' => 'Required|Min:3|Max:255|alpha_spaces', 
         'Image-description' => 'Required|Min:3', 
         'Image-file' => 'image', 
        ); 

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

      if ($validator->fails()) { 
       return \Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]); 
      } else { 
         $post = Post::create(array(
          'user_id' => Auth::user()->id, 
          'title'  => Input::get('Image-title'), 
          'description' => Input::get('Image-description'), 
          'type'  => 'Image', 
         )); 

         $file = Input::file('Image-file'); 

         $destinationPath = 'img/'; 
         $extension   = $file->getClientOriginalExtension(); 
         $filename   = 'usr_'. Auth::user()->id . '_post'.$post->id .'.'. $extension; 

         $file->move($destinationPath, $filename); 
         $post->imgLocation = $filename; 
         $post->save(); 

       DB::table('board_post')->insert(['board_id' => 2, 'post_id' => $post->id]); 

       return \Response::json(['success' => true]);//*/ 

      } 
     } 
    } 
+0

你使用CSRF過濾器嗎?是這樣的,你需要將令牌傳遞給你的ajax發佈數據。 – Philip

回答

0

檢查你在php.ini

Echo ini_get ("upload_max_filesize"); 
的upload_max_filesize

請參閱this

+0

這不是問題,我把這個值改爲2G,然後用2M的小圖片。 – Kiwi

3

您的app/storage/logs/laravel.log文件中應出現錯誤,指出確切的錯誤。在開發環境中設置PHP以顯示所有錯誤是最好的選擇,所以您可能需要考慮打開這些錯誤,所以您會得到比「500內部服務器錯誤」更具描述性的信息。

0

的問題是由於一個事實,即jQuery的/ JS真的不支持圖片上傳

我固定的,當你點擊按鈕的問題,形式獲取的驗證(通過AJAX),並顯示在錯誤有,否則它正常形式發送(如果我不會做event.prefentdefault()

這現在的作品,或許我會再看看它使它完全AJAX,但可能不會:)