最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dropzone is not defined - Stack Overflow

programmeradmin4浏览0评论

I am quite new at JavaScript and this is driving me crazy.

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

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

Then I created the form like that:

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

And it works fine. It points it to php function and I handle upload on server site.

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

// "myAwesomeDropzone" is the camelized version of the HTML element's 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 get

Uncaught ReferenceError: Dropzone is not defined

Any help will be appreciated, thanks

I am quite new at JavaScript and this is driving me crazy.

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

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

Then I created the form like that:

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

And it works fine. It points it to php function and I handle upload on server site.

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

// "myAwesomeDropzone" is the camelized version of the HTML element's 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 get

Uncaught ReferenceError: Dropzone is not defined

Any help will be appreciated, thanks

Share Improve this question edited Apr 9, 2019 at 13:54 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked Nov 20, 2015 at 21:00 zachuzachu 6912 gold badges7 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

Your code might run too soon. Wrap it in:

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

or, better (runs sooner than above code):

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

or, if you use jQuery:

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

Follow this:

Your HTML file:

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

Your JS file:

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)

  1. 暂无评论