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,
]);
}
Kuyusu (Onlar bir iç çerçeveye sahip yeni bir form oluşturmak ve gönderin) bir ajaxfileuploader eklentileri kullanabilirsiniz, bunu çözer, teşekkürler! –