Javascript 未定义 Dropzone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33835985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dropzone is not defined
提问by zachu
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 hereand 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. When I do
问题是当我想访问 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(); }
}
};
I get
我得到
Uncaught ReferenceError: Dropzone is not defined
未捕获的 ReferenceError:未定义 Dropzone
Any help will be appreciated, thanks
任何帮助将不胜感激,谢谢
回答by trincot
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
});
回答by Rana_S
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...
}
};
};