Dropzone未定义

时间:2015-11-20 21:00:34

标签: javascript jquery dropzone.js

我是JavaScript的新手,这让我发疯。

我想使用Dropzone.js,所以我从here下载了文件dropzone.js并将其包含在我的视图中,如下所示:

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

然后我创建了这样的表单:

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

它工作正常。它指向php函数,我在服务器站点上处理上传。

问题是我想在JS中访问dropzone对象来配置它。 当我做的时候

// "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(); }
  }
};

我得到了

  

未捕获的ReferenceError:未定义Dropzone

任何帮助将不胜感激,谢谢

2 个答案:

答案 0 :(得分:9)

您的代码可能会过早运行。包裹它:

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

或者,更好(比上面的代码早运行):

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

或者,如果您使用jQuery

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

答案 1 :(得分:0)

按照:

您的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...
            }
    };
};