I'm trying to save multiple files, but the Request file() method is returning null. Help-me please
My front end:
<input type="file" name="arquivos" multiple />
External JS:
...
var form = document.forms['frmDados'];
...
In one function:
...
var formData = new FormData();
var ins = form.elements['arquivos'].files.length;
for (var x = 0; x < ins; x++) {
formData.append('arquivos[]', form.elements['arquivos'].files[x]);
}
// https://stackoverflow.com/questions/35192841/fetch-post-with-multipart-form-data
await fetch(window.location.href, {
method: method, // POST or PUT
body: formData,
headers: {
'X-CSRF-Token': form.elements['_token'].value
},
}).then((response) => {
...
My backend:
$arquivos = $request->file('arquivos'); // Illuminate\Http\Request
Log::info($arquivos);
Log::info($request->all());
My laravel.log: (I uploaded 2 files, but only shows 1 in the all() method)
[2020-04-07 14:40:11] local.INFO:
[2020-04-07 14:40:11] local.INFO: array (
'otheratribute' => '3',
'otheratribute2' => '1',
'otheratribute3' => '2020-03-03',
'otheratribute4' => '03:00',
'otheratribute5' => 'Tetandos',
'otheratribute6' => '1',
'otheratribute7' => '2020-04-01',
'arquivos[]' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'jgcjkpmefepjgcpg.png',
'mimeType' => 'image/png',
'size' => 392328,
'error' => 0,
'hashName' => NULL,
)),
)
What I intend to do:
try {
$infracao->upload($arquivos, true);
} catch (\Exception $e) {
return response($e->getMessage(), 503);
}
UPDATE 07/04/2020 18:21 GMT-3
I forgot to specify, this solution worked for POST Request, but not for PUT Request which is my problem, which is this and my beginning of solution is this.
I'm using the solution as middleware, and the result for the request is what I put in the question.