Dropzone not defined

I am completely new to JavaScript and it drives me crazy.

I want to use Dropzone.js, so I downloaded the dropzone.js file from here and included it in my view as follows:

<script src="<?php echo JS_DIRECTORY; ?>/dropzone.js"></script>

Then I created this form:

<form action="http://localhost/project/uploadTest/upload" class="dropzone">
</form>

And it works great. This points to the php function, and I am uploading to the server site.

The problem is when I want to access the dropzone object in JS in order to configure it. When i do

// "myAwesomeDropzone" is the camelized version of the HTML element ID
Dropzone.options.myAwesomeDropzone = {
  paramName: "file", // The name that will be used to transfer the file
  maxFilesize: 2, // MB
  accept: function(file, done) {
    if (file.name == "justinbieber.jpg") {
      done("Naha, you don't.");
    }
    else { done(); }
  }
};

I got

Uncaught ReferenceError: Dropzone undefined

Any help would be appreciated, thanks

+6
source share
2 answers

Your code may run too fast. Wrap it in:

window.onload = function() {
    // access Dropzone here
};

or, better (faster than the code above):

document.addEventListener("DOMContentLoaded", function() {
    // access Dropzone here
});

, jQuery:

$(function() {
    // access Dropzone here
});
+9

:

HTML :

<form action="your url" class="dropzone" id="dropzone-form">
</form>

JS :

window.onload = function() {
    // dropzoneFormis the configuration for the element that has an id attribute
    // with the value dropzone-form (or dropzoneForm)
    //initialize the dropzone;
    Dropzone.options.dropzoneForm = {
            autoProcessQueue: 'your value',
            acceptedFiles: 'your value',
            maxFilesize: 'your value',
            ....and so on.
            init: function() {
               myDropzone = this;

               this.on('addedfile', function(file) {
                   //todo...something...
               }
               //catch other events here...
            }
    };
};
0

Source: https://habr.com/ru/post/1616808/


All Articles