2

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

3
  • 1
    Not that familiar with dropzone but doesn't your form element need enctype="multipart/form-data" and your route needs a post method, not get? Commented Feb 4, 2016 at 16:00
  • 1
    It has that part sorry I forgot to write it. edited Commented Feb 5, 2016 at 9:13
  • Are you using POST or GET to upload the image and form data? Since GET is limited to ~2000 characters, perhaps combined with all your other form data, something is being truncated? Commented Feb 5, 2016 at 16:08

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Tried your solution but no effect. How can I check if the file is actually uploaded? In the network part of the developer tools it says data:image/jpeg:base64... and data:image/png:base64... when I put some image on dropzone.
I've added a chunk of the code form my controller might help. If your seeing that in the network activity and getting a 200 OK as the response I'd expect it to be working.
It doesn't work. Dropzone shows progress bar OK as the file is uploaded and network activity as well. Then I click submit button on my form and it passes by if ($request->hasFile('file') && $request->file('file')->isValid()) and continues.
As the $request doesn't have a file variable
This is what I get on network activity: UploadedFile {#31 -test: false -originalName: "multimedija.jpg" -mimeType: "image/jpeg" -size: 145446 -error: 0 path: "/tmp"...
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.