Gogo
Gogo

Reputation: 603

Integrating dropzone.js with laravel

I'm trying to integrate dropzone.js with laravel 5 but I get empty FileBag when trying to dd($request).

In my view:

<form action="create-submit" class="dropzone" enctype="multipart/form-data">...</form>

I included dropzone.css and basic.css, also I included dropzone.js in my HTML.

Route:

Route::get('content/create-submit', [
    'as' => 'create-submit', 'uses' => 'ContentController@createSubmit'
]);

Controller:

public function createSubmit (Request $request) {
    dd($request);
}

Am I missing something?

Is there an easy solution to integrate dropzone.js to laravel? Pls help

Upvotes: 2

Views: 1051

Answers (2)

Ikong
Ikong

Reputation: 2570

try manually initialize your dropzone like

jQuery(document).ready(function () {    
var myDropzone = new Dropzone("#my-dropzone", { 
                url: "/dropzone",
                headers: {
                   'x-csrf-token': document.querySelectorAll('meta[name=csrf-token]')[0].getAttributeNode('content').value,
                }
              });
});

and in your controller

public function dropzone(Request $request)
    {
        $img    = $request->file('file'); //access dropzone files   
    }

Also, if no header token passed you will probably will not get any request.

For more detailed integration with laravel dropzone you can refer to this tutorial Integrating Dropzone.js in Laravel 5 applications

Upvotes: 0

Mark Davidson
Mark Davidson

Reputation: 5493

Are you initializing dropzone in JS? It should do so automatically but sometimes it doesn't work as expect.

So I'm doing this.

<div class="dropzone" id="fileUpload" class="clear">
</div>

<script type="text/javascript" defer="true">
    var token = "{{ Session::getToken() }}";
    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone("div#fileUpload", {
        url: "{{ route('upload') }}",
        params: {
            _token: token
        }
    });
</script>

We are passing a CSRF token as part of the request in this case as well.

I'd suggest using web developer tools to see what response your getting when the upload is sent to the server.

UPDATE Controller code:

if ($request->hasFile('file') && $request->file('file')->isValid()) {
        $file = $request->file('file');
        dd($file);
}

Upvotes: 1

Related Questions