2016-03-29 15 views
1

Ich habe Probleme mit der Validierung dieses AJAX POST für einen Datei-Upload. Hier ist, was ich bisher habe. Die 'erforderliche' Validierung kommt immer wieder und sagt mir, dass ich ein Bild auswählen soll. Wenn ich die js-Variable mit dem Bild console.log() dekomprimiere, zeigt es den Dateipfad korrekt an.Laravel AJAX Image Upload Validierung

Ausblick:

<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() { 

     }  
    }); 
}); 

Controller:

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, 
    ]); 
} 

Antwort

-1

dies ein beabsichtigtes Verhalten ist, kann nicht Sie Upload-Datei über XMLHTTPRequest senden, das ist, warum werden Sie immer erhalten die Bilddatei ist erforderlich Validierung.

können Sie eine ajaxfileuploader Plugins verwenden (Sie schaffen eine neue Form mit in einem Iframe und abschicken)

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

+0

Nun, das es löst, danke! –