2016-03-29 28 views
1

Bir dosya yükleme işlemi için bu AJAX POST ürününde doğrulama ile ilgili sorun yaşıyorum. Şimdiye kadar sahip olduğum şeyler. 'Gerekli' doğrulama, bir görüntü seçmemi söyleyerek geri geliyor. Js değişkenini görüntüyle konsol.log() yaptığımda, dosya yolunu doğru olarak gösterir.Laravel AJAX Resim Yükleme Doğrulama

Görünüm:

<form role="form" method="post" action="{{ route('profile.edit') }}" enctype="multipart/form-data"> 
    <div class="form-group new-pic{{ $errors->has('profile-image') ? ' has-error' : '' }}"> 
     <input type="file" id="newProfilePic" name="profile-image"/>   
    </div> 
</form> 

JS:

$('.btn-edit-profile-pic').click(function(e){ 
    e.preventDefault(); 

    var newPic = $('#newProfilePic').val(); 

    $.ajax({ 
     type: "POST", 
     url: "/profile-edit", 
     data: newPic, 
     error: function(data) { 
      var errors = $.parseJSON(data.responseText); 
      console.log(errors); 
     }, 
     success: function() { 

     }  
    }); 
}); 

Kontrolör: neden olacak

if ($request->ajax()) 
{ 
    $this->validate($request, [ 
     'newPic' => 'required|image|max:4999' 
    ],[ 
     'required' => 'You must select an image', 
     'image' => 'The file must be an image', 
     'max' => 'The image file size must be less than 5mb' 
    ]); 

    $extension = Input::file('profile-image')->getClientOriginalExtension(); 
    $fileName = rand(11111,99999).'.'.$extension; 

    $image = Image::make(Input::file('profile-image')) 
     ->orientate() 
     ->resize(300, null, function ($constraint) { 
      $constraint->aspectRatio(); 
     }) 
     ->save('images/profiles/'.$fileName); 

    Auth::user()->update([ 
     'image_path' => $fileName, 
    ]); 
} 

cevap

-1

bu, yani bir amaçlanan davranış, şu XMLHTTPRequest üzerinde yükleme dosyası göndermek cant edilir her zaman alınan görüntü dosyası doğrulama gerektirir.

Eğer

[http://www.sitepoint.com/10-jquery-ajax-file-uploader-plugins/]

+0

Kuyusu (Onlar bir iç çerçeveye sahip yeni bir form oluşturmak ve gönderin) bir ajaxfileuploader eklentileri kullanabilirsiniz, bunu çözer, teşekkürler! –