HTML表单应该像下面。请注意,
类= “悬浮窗”
和
ID = “我-悬浮窗”
“我 - 悬浮窗” ID是很重要的, DropZone将此视为“myDropzone”(连字符已删除并显示)。所以当你在DropZone.options中引用这个ID时,它应该是“myDropzone”。这个非常重要。
<div>
<form action="/file-upload" class="dropzone" id="my-dropzone" style="border:2px dotted #337ab7">
<div class="dz-message">
Drag n drop photos here or click to upload.
<br />
<span class="note">(The photos are uploaded to twitter only when the tweet is sent.)</span>
</div>
</form>
</div>
你应该链接的CSS和JS文件在HTML页面中,像这样
<script src="./js/dropzone.min.js"></script>
<link href="./css/dropzone.min.css" rel="stylesheet" type="text/css">
在页面加载,你应该初始化您的JS代码的悬浮窗的配置为你的页面,如下。我限制dropzone只接受一个文件,其大小不应超过5MB。
self.options.maxFilesize = 5; self.options.maxFiles = 1;
Dropzone.autoDiscover = false;
Dropzone.options.myDropzone = {
init: function() {
var self = this;
// config
self.options.addRemoveLinks = true;
self.options.autoProcessQueue = false;
self.options.maxFilesize = 5;
self.options.maxFiles = 1;
self.options.dictFileTooBig =
"Twitter do not allow files greater than 5MB.";
//New file added
self.on("addedfile", function(file) {
console.log('new file added ', file.path);
});
self.on("maxfilesexceeded", function(file) {
alert(
"Only one photo can be attached with the tweet."
);
});
}
};
现在,当你的页面加载,你会看到形式&得到拖N的你的DIV拖放功能。
要访问这些文件,您可以在javascript中的某些按钮单击事件中执行下面的操作。
// to get queued files
var myDropzone = Dropzone.forElement("#my-dropzone");
var files = myDropzone.getQueuedFiles();