Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropzone CSRF token mismatch Laravel 5

I am using Dropzone to upload images using Laravel 5. After Dropzone makes the put call to my URL I get the following error:

TokenMismatchException in VerifyCsrfToken.php line 67:

However, when I look at the payload for the request the token is present:

------WebKitFormBoundary91A7BYrMsDcGTEvx Content-Disposition: form-data; name="_method"

PUT ------WebKitFormBoundary91A7BYrMsDcGTEvx Content-Disposition: form-data; name="_token"

j3NbjibYF7k8g2w1P0enw6YVfDrDvCGKFMCFt4NX ------WebKitFormBoundary91A7BYrMsDcGTEvx Content-Disposition: form-data; name="title"

Here is my JS:

    Dropzone.options.realDropzone = {
        url: '/user/manage/10',
        method: 'PUT',
        paramName: 'file',
        uploadMultiple: false,
        parallelUploads: 100,
        previewsContainer: '#dropzonePreview',
        addRemoveLinks: true,
        maxFiles: 10,
        autoProcessQueue: false,

        init: function () {
            var dropZone = this;

            this.element.querySelector("#save").addEventListener("click", function (e) {
                e.preventDefault();
                e.stopPropagation();

                console.log("clicked submit");
                dropZone.processQueue();
            });
        },
    };

My form:

{!! Form::model($asset, ['method' => 'PUT', 'class' => 'dropzone', 'id' => 'real-dropzone', 'action' => ['UserManagementController@update', $asset->id], 'file' => true]) !!}

My controller:

   public function update(Request $request, $id)
    {

        return dd(FileRequest::file('file'));
    }
like image 901
Gabe Levasseur Avatar asked Jun 14 '16 16:06

Gabe Levasseur


4 Answers

Try to add the token in your Dropzone options:

sending: function(file, xhr, formData) {
    formData.append("_token", "{{ csrf_token() }}");
},
like image 73
Clemen Avatar answered Nov 03 '22 05:11

Clemen


You can just add {{ csrf_field() }} in your form

like image 5
Jean-Marc Amon Avatar answered Nov 03 '22 06:11

Jean-Marc Amon


My working code for Laravel 5.6:

HTML

<div class="dropzone dropzone-previews" id="my-awesome-dropzone"></div>

jQuery

Dropzone.autoDiscover = false;
$(document).ready(function() {
    var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    $("div#my-awesome-dropzone").dropzone({
        url: "{{ url('/upload') }}",
        headers: {
            'x-csrf-token': CSRF_TOKEN,
        },
    });
});
like image 4
Payam Khaninejad Avatar answered Nov 03 '22 05:11

Payam Khaninejad


HTML code in a blade file

<head>
    …
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
    <div class="dropzone" id="my-dropzone"></div>
</body>

JavaScript

Dropzone.autoDiscover = false;
new Dropzone('#my-dropzone', {
    url: '/upload',
    headers: {
        'x-csrf-token': document.head.querySelector('meta[name="csrf-token"]').content,
    },
});
like image 2
Samuel De Backer Avatar answered Nov 03 '22 06:11

Samuel De Backer