![](/img/trans.png)
[英]How to upload and display only one file in Dropzone.js and jsGrid?
[英]dropzone upload only one file
我将 dropzone.js 用于我的拖放文件上传解决方案。 我只想上传一个文件,如果我上传第二个文件,第一个文件应该被删除,第二个文件应该被上传。 知道怎么做..
这是我的 html
<form class="dropzone dz-clickable" action="upload.php" enctype='multipart/form-data'>
<i class="fa fa-cloud-upload element"></i>
<div style="color:gray;">Drag and drop or click to upload image</div>
<input type="hidden" name="filenameEmail" class="filenameEmail" value="">
<input type="hidden" name="side" value="front">
</form>
我改变了 dropzone.js
maxFiles: 1
它只允许上传一个文件,但我无法删除以前上传的文件。请帮助我。提前谢谢
maxFiles: 1用于告诉 dropzone 应该只有一个文件。 当有超过 1 个文件时,函数maxfilesexceeded将被调用,超出的文件作为第一个参数。
这是一个删除第一个文件预览并添加新文件的简单功能:)
maxFiles:1,
init: function() {
this.on("maxfilesexceeded", function(file) {
this.removeAllFiles();
this.addFile(file);
});
}
我使用事件maxfilesexceeded
和方法addFile
并进入事件调用的无限循环。 应该是使用addFile
的问题,因为我在官方站点或 GitHub wiki 中都没有看到它。 最后我用addedfile
事件解决了。 Dropzone.js 是我编写时的最新版本 (4.3.0)。
init: function() {
this.on('addedfile', function(file) {
if (this.files.length > 1) {
this.removeFile(this.files[0]);
}
});
}
将maxFiles
限制为 1 仍然允许在上传对话框中选择多个文件。 要禁止在配置中选择多个图像,请指定以下 init 函数:
maxFiles:1,
init: function() {
this.hiddenFileInput.removeAttribute('multiple');
}
Dropzone.prototype.defaultOptions.init = function () {
this.hiddenFileInput.removeAttribute('multiple');
this.on("maxfilesexceeded", function (file) {
this.removeAllFiles();
this.addFile(file);
});
};
这对我来说很实用。
两种解决方案的组合在 init 函数中为我完成了这项工作:
this.on("addedfile", function (file) {
if (this.files.length > 1) {
this.removeAllFiles()
this.addFile(file);
}
});
这对我有用
const dropzoneInput = document.querySelectorAll('.dz-hidden-input')
for (const input of dropzoneInput) {
if (input.getAttribute('multiple')) {
input.removeAttribute('multiple')
}
}
它所做的是验证 dropzone 输入是否具有多个属性,如果有,则删除 id
注意:class .dz-hidden-input可能与您不同。
这些解决方案都不适合我。
maxfilesexceeded
事件被调用太晚,即在尝试添加文件之后。
使用this.removeFile(this.files[0]);
其他解决方案this.removeFile(this.files[0]);
导致"Uncaught TypeError: Cannot read property 'removeChild' of null".
或无限循环。
解决了-
maxFiles: 2,
init: function () {
this.on("addedfile", function (file) {
if (this.files.length > 1) {
this.files = this.files.slice(1, 2);
}
});
}
使用dz-clickable
(文件选择器 btn)并拖放时有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.