Route
Route::get('/mediafile', 'MediaController@index');
MediaController
public function store(Request $request)
{
$image = $request->file('UserFile');
if($request->hasFile('UserFile')) {
$filename = $image->getClientOriginalName();
Image::make($image)->resize(300, 300)->save(public_path('media/' . $filename));
$media = new Media();
$media->Description = $request->Description ? $request->Description : '';
$media->MediaPath = $filename;
$media->MediaName = $filename;
$media->save();
}
return response()->json($media);
}
View
<div class="form-group image">
<div class="col-xs-12 col-sm-12 col-md-12">
<label>Gambar</label><input type="file" name="UserFile" id="userfile" style="display: inline">
<label style="margin-left: 25px">atau</label>
<a href="http://192.168.1.12/melody4/media/select.html" class="pilihmedia btn btn-default">Pilih dari media</a>
<input type="hidden" id="MediaID" name="CategoryMediaID" value="" />
<br /><br />
<span class="uploadstatus"></span>
<div class="success infomedia infomedia alert alert-success" style="display: none">
</div>
</div>
Ajax
$('#userfile').change(function(){
$(this).attr('disable',true);
$('.uploadstatus').html('Sedang mengupload file <img src="{{ asset('vendor/img/loading.gif') }}" alt="ajaxloading" />');
$(this).parent().ajaxSubmit({
url: "<?php echo url('/mediafile') ?>",
dataType: 'json',
type: "POST",
success : function(data){
$(this).attr('disable',false);
$('.uploadstatus').empty();
},
error : function(data){
}
});
});
I got an error "405 Method Not Allowed" with using AJAX when I posted an image, I have researched and followed all the answers in the Stack Overflow, but I still can't resolve it.
Please help me to solve it.
May be issue in csrf token Add this line in html
and in ajax
and need to post image in ajax call