您的 html 表单应如下所示。请注意,
class="dropzone"
和
id="my-dropzone"
“my-dropzone”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>
你应该像这样在你的html页面中链接css和js文件
<script src="./js/dropzone.min.js"></script>
<link href="./css/dropzone.min.css" rel="stylesheet" type="text/css">
在页面加载时,您应该在您的页面的 JS 代码中初始化 dropzone 配置,如下所示。我将 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."
);
});
}
};
现在,当您的页面加载时,您将看到表单并在您的 div 中获得拖放功能。
要访问文件,您可以在 javascript 中对某些按钮单击事件执行如下操作。
// to get queued files
var myDropzone = Dropzone.forElement("#my-dropzone");
var files = myDropzone.getQueuedFiles();