给出的空对象:laravel 5.1 ajax文件上传

时间:2016-07-03 17:13:49

标签: ajax file-upload laravel-5.1

{!! Form :: open(['route'=>'events.store','id'=>'form-create-event','name'=>'form-create-event','files'= > true])!!}     

  <div class="card-body card-padding">

    <div class="row">
      <div id="step1" class="col-sm-4">
        <!-- <h3 class="c-gray m-b-15">principales information</h3> -->
        <div class="round round-lg blue">
          <span>1</span>
        </div>
        <div class="form-group fg-float">
          <div class="fg-line">
            <p class="f-200 m-b-5 c-gray">Type d'évènement</p>
            <select id="type_event" name="type_event" class="tag-select" data-placeholder="">
              @foreach($types_events as $value)
              <option value="{!! $value->id !!}" >
                {{$value->name}}
              </option>
              @endforeach
            </select>
          </div>
        </div>
        <div class="form-group fg-float">
          <div class="fg-line">
            <input type="text" class="input-sm form-control fg-input" data-rule-minlength='2' required="false" id="event_name" name="event_name">
          </div>
          <label class="fg-label">Nom de l'évènement</label>
        </div>
        <!-- <div class="form-group fg-float">
          <div class="fg-line">
            <input type="text" class="input-sm form-control fg-input input-mask" required="true" data-mask="0000-00-00" name="event_begin_date" id="event_begin_date">
          </div>
          <label class="fg-label">Date début (expl 2016-01-30)</label>
          </div> -->
        <div class="input-group form-group">
          <span class="input-group-addon"><i class="md md-event"></i></span>
          <div class="dtp-container dropdown fg-line">
            <input type='text' name="event_begin_date" id="event_begin_date" class="form-control date-picker" data-toggle="dropdown" placeholder="Date début">
          </div>
        </div>
        <!-- <div class="form-group fg-float">
          <div class="fg-line">
            <input type="text" class="input-sm form-control fg-input input-mask"  required="false" data-mask="0000-00-00" id="event_end_date" name="event_end_date">
          </div>
          <label class="fg-label">Date fin (expl 2016-01-31)</label>
          </div> -->
        <div class="input-group form-group">
          <span class="input-group-addon"><i class="md md-event"></i></span>
          <div  class="dtp-container dropdown fg-line">
            <input type='text' id="event_end_date" name="event_end_date" class="form-control date-picker" data-toggle="dropdown" placeholder="Date fin">
          </div>
        </div>
        <div class='form-group fg-float'>
          <div class="fileinput fileinput-new" data-provides="fileinput">
            <span class="btn btn-primary btn-file m-r-10">
            <span class="fileinput-new">Ajouter un document</span>
            <span class="fileinput-exists">Modifier</span>
            <input type="file" id="event_document" name="event_document">
            </span>
            <span class="fileinput-filename"></span>
            <a href="#" class="close fileinput-exists" id="cancelFileUpload" data-dismiss="fileinput">&times;</a>
          </div>
        </div>



        <!-- <div id="event_document_tag_name" class="form-group">

            <input type="text" placeholder="Tag" id="event_document_tag" name="event_document_tag" class="form-control">

        </div> -->

        <div class="form-group fg-float">
          <div class="fg-line">
            <textarea name="event_description"
              id="event_description" required="false" class="form-control">
            </textarea>
          </div>
          <label class="fg-label">Description</label>
        </div>
        <div class="form-group fg-float">
          <div class="fg-line">
            <textarea id="event_info_pr" name="event_info_pr" required="false" class="form-control">
            </textarea>
          </div>
          <label class="fg-label">Informations pratiques</label>
        </div>
        <div class="form-group fg-float">
          <div class="fg-line">
            <textarea name="event_adress" required="false" id="event_adress" class="form-control">
            </textarea>
          </div>
          <label class="fg-label">Adresse</label>
        </div>
      </div>
      <div id="step2" class="col-sm-4">
        <!-- <h3 class="c-gray m-b-15">informations</h3> -->
        <div class="round round-lg blue">
          <span>2</span>
        </div>
        <div class="form-group fg-float">
          <div class="fg-line">
            <p class="f-200 m-b-5 c-gray">Invités
              <a data-toggle="modal" href="#modalDefaultUploadUsers" class="btn btn-success btn-xs waves-effect pull-right">
              <i class="md md-file-upload"></i>
              </a>
              <a style="margin-right: 10px;" data-toggle="modal" href="#modalDefaultAddUser" class="btn btn-success btn-xs waves-effect pull-right">
              <i class="md md-add"></i>
              </a>
            </p>
            <select multiple id="event_publics" name="event_publics[]" class="tag-select" data-placeholder="">
              @foreach($users as $value)
              <option value="{!! $value->id !!}" >
                {{$value->firstname}} {{$value->lastname}}
              </option>
              @endforeach
            </select>
          </div>
        </div>
        <div class="form-group fg-float">
          <div class="fg-line">
            <p class="f-200 m-b-5 c-gray">Intervenants 
            </p>
            <select id="event_speakers" name="speakers[]" class="tag-select" multiple data-placeholder="">
              @foreach($users as $value)
              <option value="{!! $value->id !!}" >
                {{$value->firstname}} {{$value->lastname}}
              </option>
              @endforeach
            </select>
          </div>
        </div>
        <!-- Groupe et participants -->
        <div class="form-group fg-float">
          <div class="fg-line">
            <p class="f-200 m-b-5 c-gray">Groupes et participants</p>
            <!-- <select id="event_gp_participants" name="gps_participants[]" class="demo" multiple="multiple">
              @foreach($groups as $group)
              @if($group->id !== 1 && $group->id !== 2)
              <optgroup value="{{$group->id}}" label="{{$group->name}} ">
                @foreach($users as $value)
                <option value="group[{{$group->id}}][{{$value->id}}]" >
                  {{$value->firstname}} {{$value->lastname}}
                </option>
                @endforeach
              </optgroup>
              @endif
              @endforeach
              </select> -->
            <select id="event_gp_participants" data-live-search="true" name="gps_participants[]" multiple class="selectpicker">
              @foreach($groups as $group)
              @if($group->id !== 1 && $group->id !== 2)
              <optgroup value="{{$group->id}}" label="{{$group->name}} ">
                @foreach($users as $value)
                <option value="group[{{$group->id}}][{{$value->id}}]" >
                  {{$value->firstname}} {{$value->lastname}}
                </option>
                @endforeach
              </optgroup>
              @endif
              @endforeach
            </select>
          </div>
        </div>
      </div>
      <div id="step3" class="col-sm-4">
        <!-- <h3 class="c-gray m-b-15">informations</h3> -->
        <div class="round round-lg blue">
          <span>3</span>
        </div>
        <div class="table-responsive">
          <table id="data-table-basic" class="table table-striped table-vmiddle">
            <thead>
              <tr>
                <th colspan="3"> 
                  <a class="pull-right" data-toggle="modal" href="#modalWider">
                  Ajouter séance
                  </a>
                </th>
              </tr>
            </thead>
            <tbody>
              <tr></tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <button type="submit" class="btn btn-info"> Enregistrer 
    </button>
    {!! link_to_route('events.index', 'Annuler', [], ['class' => 'btn btn-link']) !!}
  </div>
</div>
{!! Form::close() !!}

Js代码

var form = document.forms.namedItem(“form-create-event”);     的console.log(形式);

form.addEventListener('submit', function(ev) 
{

  oData = new FormData(form);
  oData.append("seances", seance);

  console.log(oData);

  var oReq = new XMLHttpRequest();
  oReq.open("POST", form.action, true);
  oReq.onload = function(oEvent) {
    if (oReq.status == 200) {
      console.log('success');
    } else {

        console.log('failed');

    }
  };

  oReq.send(oData);
  ev.preventDefault();
}, false);

php code

公共功能商店(请求$请求)     {        return response() - &gt; json($ request-&gt; all());

}

===========================================

我的问题是:

当我尝试(dd($ request-&gt; event_document))返回空对象

header request

Server response

1 个答案:

答案 0 :(得分:0)

很可能您正在尝试上传大于php.ini允许的大小的文件。所以在你的php.ini中设置upload_max_filesize和post_max_size的值:

上传文件的最大允许大小。

upload_max_filesize = 10M或您需要的任何尺寸

必须大于或等于upload_max_filesize

post_max_size = 10M