DropZone.js - 选择文件后立即在屏幕上抛出错误

DropZone.js - Throwing an error on screen immediately after a file is selected

我正在使用 dropzone.js,并试图弄清楚如何在文件显示在拖放区之前抛出错误 UI。

 Dropzone.autoDiscover = false; 
     var myDropzone = new Dropzone("#fileUpload", { url: "/myUrl/file", maxFiles: 1, maxFilesize: 2,
                    accept: function(file, done) {            
                                    var splitFileName = file.name.split(".");                    
                                    if (splitFileName[splitFileName.length - 1] != "bbb") {                                                                        
                                        done("Error! Files of this type are not accepted");                                    
                                    }
                                    else { done(); }
                                }                         
                    });
 myDropzone.options.acceptedFiles = '.bbb';

假设用户拖动一个不带 .bbb 扩展名的文件,那么该文件仍显示在拖放区中(带有 'X' 符号)。

是否可以不在拖放区中显示该文件,甚至显示相关错误?

我发现您可以使用有用的 removeFile 方法和 addedfile 事件来轻松实现这种功能。

myDropzone.on("addedfile", function(file) {
});

在此处理程序中,您可以删除具有 .bbb 扩展名的文件并设置您希望的任何类型的行为。