Lonare's avatar

Laravel 4.2 and Dropzone Js

Hi @bashy

I have created a dropzone form on my page:

<form class="dropzone dz-clickable" id="dropzonetask" enctype="multipart/form-data" method="POST" action="{{ URL::route('upload-file') }}">
                              <input type="hidden" name="task_id" value="{{ $task->id  }}" />
                            <div class="dz-message"><center>
                            <h4>Drag Photos to Upload</h4>
                                <span>Or click to browse</span></center>
                            </div> 
                            <button id="uploadall" class="btn btn-default pull-right">Upload All</button> 
                          </form>

you can see i have added an hidden field here.

but when i drag n drop a file on this form it upload the file successfully but wont get task_id value.

here is my controller

 public function UploadFile(){  
        $taskid             = Input::get('task_id');   
        $userid             = Auth::user()->id; 

        $file               = Input::file('file'); 
        $destinationPath    = public_path().'files';
        $filename           = $file->getClientOriginalName();
        $extension          = $file->getClientOriginalExtension();  
        $uploadSuccess      = $file->move($destinationPath, $filename);
        $location           = $destinationPath."/".$filename.$extension;
        if( $uploadSuccess ) {
           $taskfile                = TaskFile::create(array(
                                         'task_id' => $taskid,
                                         'user_id' => $userid,
                                         'location' => $location
                                        ));  
           if($taskfile){
              return Response::json('success', 200);    
           }else{
              return Response::json('error', 400);  
           }

        } else {
           return Response::json('error', 400);
        }

    }

anything i am doing wrong ?

0 likes
7 replies
noeldiaz's avatar

Hum, that looks basically identical to one I have. Don't see why the hidden field would not be passed on.

You already checked that the value for that hidden field is being set on the page? Like by viewing the page source? Can you paste the controller action that calls the view with the form?

jehad's avatar

Why mess around with Dropzone config and why add a button when all you need to do is change the route you're posting to to contain the task_id param? So you can just post to /tasks/{id}/files and use that in your controller, without the need for a hidden input field.

EDIT: reading the docs, it seems Dropzone automatically submits any hidden fields within the form: https://github.com/enyo/dropzone/wiki/FAQ#how-to-submit-additional-data-along-the-file-upload , so your code should work. Not sure yet why it's acting up on you...

EDIT 2: There's a gh issue regarding this: https://github.com/enyo/dropzone/issues/154 , so it may be a bug - maybe one introduced recently. Maybe @noeldiaz can share the DZ version he's using and you can try that one?

bashy's avatar

I used this for Dropzone, maybe you needed an extra input field for image of file descriptions.

myDropzone.on("sending", function(file, xhr, formData) {
    // Show the total progress bar when upload starts
    document.querySelector("#total-progress").style.opacity = "1";
    // And disable the start button
    file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");

    // Selector of file_label input
    var fileLabel = file.previewElement.querySelector('input[name="file_label"]').value;

    formData.append('file_label', fileLabel);
    formData.append('_token', '{{ csrf_token() }}');
});
noeldiaz's avatar

@jehad the version I'm using in the code I checked is over a year old. :) Still working for what it is needed so haven't updated it. Have not tested with anything newer.

jehad's avatar

But if you check the post request data using Chrome Dev tools for example, is the hidden field sent to the server?

Please or to participate in this conversation.