Did you checked the $request->file('image'); ?
This is the uploaded image, as you can see the $request->file('image')->isValid() return true.
Laravel 5.2.26 - $request->file is empty or null with jquery ajax file uploading
This is my JavaScript
$editorContainer.on('change', 'input[type=file]', function(){
var $fileInput = $(this),
file = this.files[0],
fileType = file.type.split('/')[1];
if($.inArray(fileType, availableFileTypes) > 0){
var formData = new FormData;
formData.append('image', file);
$.ajax({
url: url,
type: 'POST',
processData: false,
contentType: false,
cache: false,
data: formData,
dataType: 'JSON',
beforeSend: function(){
$loading.removeClass('hide');
},
complete: function(){
$loading.addClass('hide');
},
success: function(response){
console.log(response);
},
error: function(e,a,b){
console.log(e,a,b);
}
});
}
$fileInput.replaceWith($fileInput.clone(true));
});
This is controller method that handle current ajax request
/**
* @param Request $request
* @return array
*/
public function uploadImage(Request $request)
{
return ['all_data' => $request->all(), 'request_files' => $request->allFiles(), '$_FILES' => $_FILES, 'valid' => $request->file('image')->isValid()];
}
And this is the result
{"all_data":{"image":{}},"request_files":{"image":{}},"$_FILES":{"image":{"name":"laravel_logo.png","type":"image\/png","tmp_name":"C:\\Windows\\Temp\\phpB2AA.tmp","error":0,"size":41650}},"valid":true}
What I had done wrong?
OMG, It was my mistake, even not a mistake. I tried to return response like json with instance of UploadedFile, that has no public properties and so I could'n see it in the browser console. dd($request->file('image')); do the trick and I saw my uploaded file
Please or to participate in this conversation.