2016-11-11 76 views
0

我有一個簡單的文件上傳輸入,不會轉儲文件輸入值。在Laravel中上傳文件的問題

這是形式的代碼,我有:

<!-- Modal --> 
     <div class="modal fade" id="app-form" tabindex="-1" role="dialog" aria-labelledby="app-form-label"> 
      <div class="modal-dialog" role="document"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span 
            aria-hidden="true">&times;</span></button> 
         <h4 class="modal-title" id="app-form-label">Ședință foto boudoir!</h4> 
        </div> 

        {!! Form::open(['url'=>'','method'=>'POST', 'files'=>true, 'id' => 'model-form']) !!} 
        <div class="modal-body"> 
         <div class="row"> 
          <div class="col-xs-12 col-sm-6"> 
           <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}"> 
            {!! Form::label('first_name', 'Nume') !!} 
            {!! Form::text('first_name', '', ['class' => 'form-control']) !!} 
            @if ($errors->has('first_name')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('first_name') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 

          <div class="col-xs-12 col-sm-6"> 
           <div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}"> 
            {!! Form::label('last_name', 'Prenume') !!} 
            {!! Form::text('last_name', '', ['class' => 'form-control']) !!} 
            @if ($errors->has('last_name')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('last_name') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 
         </div> 

         <div class="row"> 
          <div class="col-xs-12 col-sm-6"> 
           <div class="form-group{{ $errors->has('age') ? ' has-error' : '' }}"> 
            {!! Form::label('age', 'Vârsta') !!} 
            {!! Form::text('age', '', ['class' => 'form-control']) !!} 
            @if ($errors->has('age')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('age') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 

          <div class="col-xs-12 col-sm-6"> 
           <div class="form-group{{ $errors->has('sex') ? ' has-error' : '' }}"> 
            {!! Form::label('sex', 'Vârsta') !!} 
            {!! Form::select('sex', ['M' => 'Masculin', 'F' => 'Feminin'], null, ['class' => 
            'form-control', 
            'placeholder' => 
         '-- Selectează --']) !!} 
            @if ($errors->has('sex')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('sex') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 
         </div> 

         <div class="row"> 
          <div class="col-xs-12 col-sm-6"> 
           <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> 
            {!! Form::label('email', 'Email') !!} 
            {!! Form::email('email', '', ['class' => 'form-control']) !!} 
            @if ($errors->has('email')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('email') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 

          <div class="col-xs-12 col-sm-6"> 
           <div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}"> 
            {!! Form::label('phone', 'Telefon') !!} 
            {!! Form::text('phone', '', ['class' => 'form-control']) !!} 
            @if ($errors->has('phone')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('phone') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 
         </div> 

         <div class="row"> 
          <div class="col-xs-12"> 
           <div class="form-group{{ $errors->has('details') ? ' has-error' : '' }}"> 
            {!! Form::label('details', 'Detalii') !!} 
            {!! Form::textarea('details', '', ['class' => 'form-control', 'rows' => 10]) !!} 
            @if ($errors->has('details')) 
             <span class="help-block"> 
              <strong>{{ $errors->first('details') }}</strong> 
             </span> 
            @endif 
           </div> 
          </div> 
         </div> 

         {!! Form::label('image', 'Fotografii Reprezentative') !!} 
         {!! Form::file('image') !!} 
        </div> 
        <div class="modal-footer"> 
         <button type="submit" class="btn btn-warning card-2">Trimite Datele</button> 
        </div> 
        {!! Form::close() !!} 
       </div> 
      </div> 
     </div> 
     <!-- /End Modal --> 

我用ajax做後,此代碼:

<script> 
    (function() { 
     $('#model-form').submit(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
       type: 'POST', 
       url: 'store', 
       headers: { 
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
       }, 
       data: new FormData(this), 
       contentType: false, 
       cache: false, 
       processData:false, 
       success: function (response) { 
        console.log($('#image').val()); 
       } 
      }); 
     }); 
    })(); 
</script> 

現在不要問我爲什麼我設置了csrf-在ajax令牌頭,這是另一個問題,但現在,問題是,我做$ _POST & $ _FILES轉儲後,我提交表單,我有除了圖像文件上傳數據的形式的所有值。 我拉着我的頭髮,所以請幫助我!

+0

轉儲$ _POST文件?你的意思是$ _FILES?你應該爲你上傳的文件轉儲$ _FILES超全球 – Andreas

+0

是的,對不起,我做了$ _FILES。 –

+0

不,沒有控制檯錯誤。 –

回答

1

你應該使用enctype=multipart/form-data每當你想發送的文件數據的形式是這樣的:

<form action="" method="POST" enctype="multipart/form-data"> 
</form> 

可以使用laravel的dd()方法轉儲輸入內部控制是這樣的:

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

希望這有助於!

+0

它由laravel中的'files'=> true設置... –

+0

謝謝! 我用dd(request) - > all()和我看到的文件... 爲什麼不用var_dump($ _ FILES)? –

+0

不知道真的但是,乾杯,爲了解決你的問題問題:D –