I'm trying to upload an image through ajax in laravel.
I have this code for the ajax.
$("#stepbutton2").focus(function(){
var formData = new FormData($("#form1"));
$.ajax({
url: '/nominations/upload/image',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data.url);
},
error: function(err){
alert(err);
}
});
});
and php file:
public function image(){
$file = Input::file('largeImage');
$ext = $file->getClientOriginalExtension();
$newName = md5(time()).".$ext";
$path= "uploads/{$this->getDate()}";
$file->move($path, $newName);
$imageUrl = $path.'/'.$newName;
return Response::json(["success"=>"true", "url"=>$imageUrl]);
}
#stepbutton2
is a button type and not submit.
I'm getting the alert [Object object]
but it should be the uploaded URL.
The markup is something like:
<form id='form1'>
<input type='file' id='largeImage' name='largeImage'>
<input type='button' id='stepbutton2'>
</form>
where did I go wrong? I need to use this url to populate another field in the same form and continue filling the form to the next steps.