2015-07-19 120 views
2

我仍在學習Laravel並試圖構建某種文章管理系統。作爲插入新文章的形式的一部分,我想也上傳他們的多個圖片。上傳顯示成功,但上傳文件夾中沒有文件。這裏是我的表單代碼:Dropzone顯示上傳成功,但文件未上傳到Laravel 5

{!! Form::open(['route' => 'admin_artikli.store', 'class' => 'dropzone', 'id' => 'create_artikal', 'enctype' => 'multipart/form-data' ]) !!} 

    <div class="form-group"> 
     {!! Form::label('Firma', 'Firma') !!} 
     {!! Form::text('firma_id', Input::old('firma_id'), array('class' => 'form-control')) !!} 
    </div> 
    <div class="form-group"> 
     {!! Form::label('Naziv', 'Naziv') !!} 
     {!! Form::text('naziv', Input::old('naziv'), array('class' => 'form-control')) !!} 
    </div> 

    <div class="form-group"> 
     {!! Form::label('Opis', 'Opis') !!} 
     {!! Form::text('opis', Input::old('opis'), array('class' => 'form-control')) !!} 
    </div> 

    <div class="form-group"> 
     {!! Form::label('Cijena', 'Cijena') !!} 
     {!! Form::text('cijena', Input::old('cijena'), array('class' => 'form-control')) !!} 
    </div> 
    <div class="form-group"> 
     {!! Form::label('kolicina', 'kolicina') !!} 
     {!! Form::text('kolicina', Input::old('kolicina'), array('class' => 'form-control')) !!} 
    </div> 
     <div class="form-group"> 
     {!! Form::label('dostupnost', 'dostupnost') !!} 
     {!! Form::text('dostupnost', Input::old('dostupnost'), array('class' => 'form-control')) !!} 
    </div> 
    <div class="form-group"> 
     {!! Form::select('aktivan', array('D' => 'Aktivan', 'N' => 'Neaktivan'), 'D') !!} 
    </div> 
    <div class="form-group"> 
     {!! Form::select('aktivan', array('D' => 'Aktivan', 'N' => 'Neaktivan'), 'D') !!} 
    </div> 

    {!! Form::submit('Kreiraj artikal', array('class' => 'btn btn-primary', 'id' => 'create_artikal_submit')) !!} 

{!! Form::close() !!} 

我的routes.php文件(只顯示相關的):

public function upload(){ 
    $input = Input::all(); 
    $rules = array(
     'file' => 'image|max:3000' 
    ); 
    echo "eldaaaaaaaaaaaaaaaaaaaaaaaaar"; 
    $validation = Validator::make($input, $rules); 

    if ($validation->fails()) 
    { 
     return Response::make($validation->errors->first(), 400); 
    } 
    $file = Input::file('file'); 
    $extension = File::extension($file['name']); 
    $directory = public_path().'uploads/'.sha1(time()); 
    $filename = sha1(time().time()).".{$extension}"; 
    $file->move($directory, $filename); 
    $upload_success = Input::file('file', $directory, $filename)->move($directory, $filename);; 
    echo $directory; 
    if($upload_success) { 
     return Response::json('success', 200); 
    } else { 
     return Response::json('error', 400); 
    } 
} 



public function store(Request $request) 
{ 

    $artikal = new Artikal; 
    $artikal->firma_id  = Input::get('firma_id'); 
    $artikal->naziv   = Input::get('naziv'); 
    $artikal->sifra   = Input::get('sifra'); 
    $artikal->opis   = Input::get('opis'); 
    $artikal->cijena   = Input::get('cijena'); 
    $artikal->kolicina  = Input::get('kolicina'); 
    $artikal->dostupnost  = Input::get('dostupnost'); 
    $artikal->aktivan  = Input::get('aktivan'); 
    $artikal->save(); 

    Session::flash('flash_message', 'Uspješno unesen artikal!'); 

    //return redirect()->back(); 

} 

最後,懸浮窗選項一:

Route::post('/upload', '[email protected]'); 
Route::resource('admin_artikli', 'ArtikalAdminController'); 

用於存儲和上傳我的控制器方法我目前使用:

Dropzone.options.createArtikal = { // The camelized version of the ID of the form element 

    // The configuration we've talked about above 
    autoProcessQueue: false, 
    uploadMultiple: true, 
    parallelUploads: 10, 
    maxFiles: 10, 

    // The setting up of the dropzone 
    init: function() { 
    var myDropzone = this; 

    // First change the button to actually tell Dropzone to process the queue. 
    var element = document.getElementById('create_artikal_submit'); 
    var form = document.getElementById('create_artikal'); 
    element.addEventListener("click", function(e) { 
     // Make sure that the form isn't actually being sent. 
     e.preventDefault(); 
     e.stopPropagation(); 
     myDropzone.processQueue(); 
    }); 

    // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead 
    // of the sending event because uploadMultiple is set to true. 
    this.on("sendingmultiple", function() { 
     // Gets triggered when the form is actually being sent. 
     // Hide the success button or the complete form. 
    }); 
    this.on("successmultiple", function(files, response) { 
     // Gets triggered when the files have successfully been sent. 
     // Redirect user or notify of success. 
     form.submit(); 
    }); 
    this.on("errormultiple", function(files, response) { 
     // Gets triggered when there was an error sending the files. 
     // Maybe show form again, and notify user of error 

    }); 
    } 

} 

我知道這是缺乏很多東西,但我一直試圖一步一步,我卡在這裏。當我檢查開發工具的網絡選項卡時,我看到XHR請求是200 OK,但是我看不到任何文件。

回答

0

你忘了寫'files'=> true在窗體中,更新表格&試試。

{!! Form::open(['route' => 'admin_artikli.store', 'class' => 'dropzone', 'id' => 'create_artikal', 'files'=>true, 'enctype' => 'multipart/form-data' ]) !!}