How to include the CSRF token in the headers in Dropzone upload request?

前端 未结 9 1818
难免孤独
难免孤独 2020-12-13 18:19

I am working on a single page application and I am using Laravel 5 for the web service.

All forms are submitted asynchronously and I use a beforeSend on them to atta

相关标签:
9条回答
  • 2020-12-13 18:43

    For anyone using default Laravel setup:

    window.Laravel = {!! json_encode([
        'csrfToken' => csrf_token(),
    ]) !!};
    
    Dropzone.options.attachments = {
        url: 'upload',
        headers: {
            'X-CSRF-TOKEN': Laravel.csrfToken
        }
    }
    
    0 讨论(0)
  • 2020-12-13 18:43
    you can add a headers.
    
    var myDropzone = new Dropzone("#drop_id", {
        url: "/upload/",
        headers: {'x-csrftoken': $.cookie('csrftoken')},
        method:"post",  
        ...
    }
    
    0 讨论(0)
  • 2020-12-13 18:47

    We can set CSRF token in request header.

     xhr = open("POST",logURL,true);
          //Set CSRF token in request header for prevent CSRF attack.
     xhr.setRequestHeader(CSRFHeaderName, CSRFToken);

    0 讨论(0)
  • 2020-12-13 18:48

    This also works pretty well :

    $("#mydropzone").dropzone({
      url: "/profile/update-photo",
      addRemoveLinks : true,
      maxFilesize: 5,
      dictResponseError: 'Error uploading file!',
      headers: {
        'X-CSRF-Token': $('input[name="authenticity_token"]').val()
      }
    });
    
    0 讨论(0)
  • 2020-12-13 18:48
    Dropzone.autoDiscover = false;
            // or disable for specific dropzone:
            // Dropzone.options.myDropzone = false;
    
            $(function () {
                // Now that the DOM is fully loaded, create the dropzone, and setup the
                // event listeners
    
                var myDropzone = new Dropzone("#my-awesome-dropzone");
                myDropzone.on("addedfile", function (file) {
                    /* Maybe display some more file information on your page */
                });
                myDropzone.on("sending", function (file, xhr, formData) {
                     formData.append('csrfmiddlewaretoken', document.getElementsByName('csrfmiddlewaretoken')[0].value);
                    /* Maybe display some more file information on your page */
                });
            });
    

    You could include it this way.

    0 讨论(0)
  • 2020-12-13 18:52

    Okay so this code is working just fine now:

    $("#mydropzone").dropzone({
        url: "/profile/update-photo",
        addRemoveLinks : true,
        maxFilesize: 5,
        dictDefaultMessage: '<span class="text-center"><span class="font-lg visible-xs-block visible-sm-block visible-lg-block"><span class="font-lg"><i class="fa fa-caret-right text-danger"></i> Drop files <span class="font-xs">to upload</span></span><span>&nbsp&nbsp<h4 class="display-inline"> (Or Click)</h4></span>',
        dictResponseError: 'Error uploading file!',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
        }
    });
    

    So basically I needed to add the X-CSRFToken in the header of the Dropzone request. Works like charm now.

    0 讨论(0)
提交回复
热议问题