Request laravel does not return input file

3

Why can not I get the View file?

I have a form that points to a route. Among other attributes, I have a file upload. This file upload is the only attribute that does not appear in request .

<label for="recipient" class="control-label">Foto:</label>
 <input id="foto" type="file" name="foto" accept="image/*" enctype="multipart/form-data">
 @if ($errors->has('foto'))
    <span class="help-block">
      <strong>{{ $errors->first('foto') }}</strong>
    </span>
 @endif
</div>

In the controller, I have:

dd($this->request->all());

This returns all attributes, but neither does the 'photo' appear.

I've already tried it:

dd($request->file('foto'));

What returns null even with a selected file.

    
asked by anonymous 21.12.2017 / 20:24

2 answers

6

Your input is correct except that enctype="multipart/form-data" is an attribute of form and not input

But with form correct this does not hinder sending

So, although you have not posted your form , the error should be in this or your route, to do a little test I put this form :

<form action="api/annex" method="post" enctype="multipart/form-data">

    <label for="recipient" class="control-label">Foto:</label>
    <input id="foto" type="file" name="foto" accept="image/*" enctype="multipart/form-data">
        @if ($errors->has('foto'))
            <span class="help-block">
            <strong>{{ $errors->first('foto') }}</strong>
            </span>
        @endif
    </div>

    <input type="submit">
</form>

And this worked correctly, as shown below:

Request {#39 ▼
  #json: null
  #convertedFiles: null
  #userResolver: Closure {#123 ▶}
  #routeResolver: Closure {#124 ▶}
  +attributes: ParameterBag {#41 ▶}
  +request: ParameterBag {#40 ▶}
  +query: ParameterBag {#47 ▶}
  +server: ServerBag {#44 ▶}
  +files: FileBag {#43 ▼
    #parameters: array:1 [▼
      "foto" => UploadedFile {#28 ▼
        -test: false
        -originalName: "Dassad.png"
        -mimeType: "image/png"
        -size: 346505
        -error: 0
        path: "/tmp"
        filename: "phpJRjAf5"
        basename: "phpJRjAf5"
        pathname: "/tmp/phpJRjAf5"
        extension: ""
        realPath: "/tmp/phpJRjAf5"
        aTime: 2017-12-28 17:26:39
        mTime: 2017-12-28 17:26:39
        cTime: 2017-12-28 17:26:39
        inode: 1079759
        size: 346505
        perms: 0100600
        owner: 33
        group: 33
        type: "file"
        writable: true
        readable: true
        executable: false
        file: true
        dir: false
        link: false
      }
    ]
  }
  +cookies: ParameterBag {#42 ▶}
  +headers: HeaderBag {#45 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/api/annex"
  #requestUri: "/apiapp/public/api/annex"
  #baseUrl: "/apiapp/public"
  #basePath: null
  #method: "POST"
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -isHostValid: true
  -isClientIpsValid: true
  -isForwardedValid: true
  basePath: "/apiapp/public"
  format: "html"
}

So check out your form if it has the attribute

enctype="multipart/form-data"

If it still does not work check out your form if you have the correct method:

method="post"

If it still does not work check your route if you have the correct method, eg:

Route::resource('annex', 'AnnexController', [
    'only' => [
            'store',
    ]
]);

or

Route::post('annex', 'AnnexController@store');
    
28.12.2017 / 20:30
0

Possibly you are using Ajax to send the data to the controller. In this case, since there is a file-type input, ajax will not send the content of the file.

Use FormData instead of serialize. Below I show a code fragment in which I send data from a form that contains an input of type file.

$('#btnAddProjecto').click(function () {
            var data = new FormData($('#myFormProjecto')[0]);// codigo antigo $('#myFormProjecto').serialize();
           
            $.ajax({
                type: 'jax',
                method: 'post',
                url: "{{ route('projecto.store')}}",
                data: data,
                processData: false,
                contentType: false,
                async: true,
                dataType: 'json',
                success: function (resposta) {
                    alert(resposta);
                },
                error: function (error) {
                    console.log(error);
                   
                }
            });
        });
    
29.12.2017 / 10:06