简体   繁体   English

Dropzone未定义

[英]Dropzone is not defined

I am quite new at JavaScript and this is driving me crazy. 我是JavaScript的新手,这让我发疯。

I want to use Dropzone.js so I downloaded the file dropzone.js from here and included it in my view like this: 我想使用Dropzone.js,所以我从这里下载了文件dropzone.js并将其包含在我的视图中,如下所示:

<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. 它指向php功能,我处理服务器站点上传。

The problem is when I want to access the dropzone object in JS to configure it. 问题是当我想访问JS中的dropzone对象来配置它时。 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 未捕获的ReferenceError:未定义Dropzone

Any help will be appreciated, thanks 任何帮助将不胜感激,谢谢

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 : 或者,如果你使用jQuery

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

Follow this: 按照这个:

Your HTML file: 你的HTML文件:

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

Your JS file: 你的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...
            }
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM