How do I send the value of a file type input by ajax to be used in the controller?

0

I have a form which sends the data to the controller through ajax

<script>
    function save() {
        $.ajax({
            type: 'POST',
            url: '{{url('/user/new')}}',
            data: {
                startDate: $('#startDate').val(),
                nota: $('#nota').val(),
                endDate: $('#endDate').val(),
                file: $('#file').val()
            }
            ,
            success: function (data) {
                // location.reload();
            },
            error: function (data) {
                showAjaxErrors(data, 'error_msg');
                markError(['startDate', 'endDate', 'nota']);
            }
        });
    }
</script>
{{ Form::open(array('url' => '/save', 'files'=> 'true'), ['id' => 'absenceForm']) }}
<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12">
        <div class="form-group">
            {{ Form::text('startDate', null, ['class' => 'form-control date', 'placeholder' => 'Fecha Inicio', 'id' => 'startDate', 'title' => 'Fecha inicio']) }}
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12">
        <div class="form-group">
            {{ Form::text('endDate', null, ['class' => 'form-control date', 'placeholder' => 'Fecha Fin', 'id' => 'endDate', 'title' => 'Fecha Fin']) }}
        </div>
    </div>
</div>


<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12">
        <div class="form-group">
            {{ Form::textarea('nota', null, ['class' => 'form-control', 'placeholder' => 'Nota', 'id' => 'note', 'rows' => '3', 'title' => 'Nota']) }}
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-10 col-md-10 col-sm-12">
        <div class="form-group">
            {{Form::file('file'),['id'=>'file']}}
        </div>
    </div>
</div>
<div class="modal-footer">
    {{ Form::button('Guardar', ['class' => 'btn btn-primary', 'onClick' => 'save()']) }}
</div>
{{Form::close()}}

When doing dd($request->all()); just show me

array: 5 [   "startDate" = > "21-11-2018"   "note" = > "Test note"   "endDate" = > "21-11-2018" ]

But the value of the file field does not show it to me And if I do dd($request->file('file')); it shows me NULL

How do I get the value of the file to use in the controller?

    
asked by gmrYaeL 21.11.2018 в 21:03
source

1 answer

0

As far as I remember it has never worked for me to send Files to Laravel so directly, what I do is create a formdata.

var formData = new FormData();

formData.append("startDate", $('#startDate').val());
...
formData.append("file", fileInputElement.files[0]);

And if I remember correctly, you should change a couple of settings in Ajax, so that something like that remains (if my memory does not fail me)

$.ajax({
  type: "POST",
  url: '{{url('/user/new')}}',
  data: formData,
  processData: false,  // tell jQuery not to process the data
  contentType: false,   // tell jQuery not to set contentType
  success: function (data) {
    // location.reload();
  },
  error: function (data) {
    showAjaxErrors(data, 'error_msg');
    markError(['startDate', 'endDate', 'nota']);
  }
});

link

    
answered by 22.11.2018 / 01:12
source