0
I have following HTML:
<meta name="csrf-token" content="{{ csrf_token() }}" />
<form action="{{route('clinic.picture.add.ajax')}}" method="post"
enctype="multipart/form-data" id="pic-form">
@csrf
<input type="file" name="files" class="form-control form-control-sm" id="add-file">
</form>
When I select the image file I run the following script
$('#add-file').change(function(){
let form = $('#pic-form')[0];
let data = new FormData(form);
// pic.append('file', $('#add-file'))
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
enctype: 'multipart/form-data',
url: "{{asset('clinica/addPicture')}}",
data: data,
processData: false,
contentType: false,
cache: false,
type: 'POST',
success: function(data){
console.log(data);
}
});
});
Rota do Laravel
Route::group(['middleware' => ['auth']], function () {
Route::prefix('clinica')->group(function () {
Route::post('/addPicture', 'Clinic\Dashboard\ClinicController@clinicAddPictureAjax')
->name('clinic.picture.add.ajax');
});
});
Method in the controller
public function clinicAddPictureAjax(Request $request)
{
\Log::alert($request);
try {
DB::beginTransaction();
$data = ClinicPictureRepository::createClinicPictures($request,
Auth::user()->id);
DB::commit();
return response()->json($data, 200);
} catch (\Exception $e) {
DB::rollBack();
\Log::alert($e->getMessage());
return response()->json($e->getMessage(), 500);
}
}
However when I execute the request to the server it is pointed out that nothing is being sent, this is the log shown in the Standard.
[2019-10-29 12:51:57] local.ALERT: array (
)
Would anyone know what’s wrong in the relationship?
Unfortunately this way the request to the server did not even occur...
– Betini O. Heleno
What is the browser window debug error by pressing F12 console tab? @Betinio.Heleno
– novic
In Laravel
 PHP;
php
array (
 '_token' => '7shXUmsknBVwilpavXqmJe1gyBjlS2hnKhINHtpZ',
 'files' => 
 Illuminate\Http\UploadedFile::__set_state(array(
 'test' => false,
 'originalName' => 'download.jpg',
 'mimeType' => 'image/jpeg',
 'error' => 0,
 'hashName' => NULL,
 )),
)

– Betini O. Heleno
No js returns a
{ }
just.... It seems that you are not sending us Multipart/form-data mode',– Betini O. Heleno
You were right... It happened that in HTML I forgot to put the
name="files[]"
so as to understand that it was an array, since my method in the Repository to save is foreach on top of several imanges.. Thank you, your answer was completely correct– Betini O. Heleno