1

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.

JhowRaul
  • 11
  • 4
  • Does `foreach ($arquivos as $arquivo) {Log::info($arquivo);}` help? – Tpojka Apr 07 '20 at 18:25
  • does not enter the loop, as $arquivos is empty – JhowRaul Apr 07 '20 at 19:47
  • Try first with `` maybe (also set other code accordingly)? – Tpojka Apr 07 '20 at 19:57
  • it didn't work, even adding [] everywhere where there are `$arquivos` the only thing I got was `[2020-04-07 17:05:40] local.INFO: /tmp/sfyTLOnk0` to `Log::info($request->file('arquivos[]'));` – JhowRaul Apr 07 '20 at 20:10
  • `/tmp/sfyTLOnk0` could be temporary file. – Tpojka Apr 07 '20 at 20:11
  • Hey friends, I forgot to specify, this solution worked for POST Request, but not for PUT Request which is my problem, which is this: github.com/laravel/framework/issues/13457 and my beginning of solution is this: gist.github.com/Stunext/9171b7a8f3633b0b601a0feb8088dca1 I will edit the question to specify – JhowRaul Apr 07 '20 at 21:21

1 Answers1

0

I think you just need to do

$request->arquivos

instead of

$request->file('arquivos')

And probably you could also do

$request->input('arquivos')
Germán Medaglia
  • 182
  • 1
  • 1
  • 7
  • I would like to emphasize that for the other attributes this works, less for the files – JhowRaul Apr 07 '20 at 20:35
  • Hey friends, I forgot to specify, this solution worked for POST Request, but not for PUT Request which is my problem, which is this: https://github.com/laravel/framework/issues/13457 and my beginning of solution is this: https://gist.github.com/Stunext/9171b7a8f3633b0b601a0feb8088dca1 I will edit the question to specify – JhowRaul Apr 07 '20 at 21:21