javascript Dropzone.js 以编程方式调用 accept() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18082011/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:40:38  来源:igfitidea点击:

Dropzone.js invoke accept() function programmatically

javascriptjqueryfile-uploaddrag-and-dropdropzone.js

提问by Juuro

I use Dropzone.jsand I implemented a custom constraint for files that rejects files with the same filename. Heres my code of the initialisation:

我使用Dropzone.js并为拒绝具有相同文件名的文件的文件实施了自定义约束。这是我的初始化代码:

var myDropzone = new Dropzone("div#myId", {
  url: MpgCommon.app.route('upload_files_path'),
  addRemoveLinks: true,
  thumbnailWidth: "80",
  thumbnailHeight: "80",
  dictCancelUpload: "Cancel",
  parallelUploads: 12,
  uploadMultiple: false,
  autoProcessQueue: false,
  accept: function(file, done) {
    var selected_filenames = $.map(myDropzone.files, function(selected_file) {
      return (selected_file.name.replace(/\.[^.]+$/, ''));
    });
    var filename = file.name.replace(/\.[^.]+$/, '')
    if ($.inArray(filename, selected_filenames) != selected_filenames.length-1){
      var error_msg = "Error!";
      $('.edit_tooltip .gui_message.error').css({display: "block"}).html(error_msg);
      done(error_msg);
    }
    else {
      $('.edit_tooltip .gui_message.error').css({display: "none"}).html('');
      done();
    }
  }
});

Now it seems that the accept function is called when a file is added but not when a file is removed from the dropzone. So is it possible to invoke the accept function programmtically?

现在似乎在添加文件时调用了 accept 函数,而不是在从放置区中删除文件时调用了该函数。那么是否可以以编程方式调用接受函数?

I tried this, but it dosn't work:

我试过这个,但它不起作用:

myDropzone.on("removedfile", function(file) {
  var rejected_files = myDropzone.getRejectedFiles();
  console.log(rejected_files);
  rejected_files.forEach(function () {
    console.log(this);
    myDropzone.accept(this);
  });
});

The error I get in the console is:

我在控制台中得到的错误是:

TypeError: done is not a function
if ( args ) {

采纳答案by enyo

I responded to this question on github: https://github.com/enyo/dropzone/issues/257

我在github上回答过这个问题:https: //github.com/enyo/dropzone/issues/257

回答by Ian Newland

This is a less than optimal solution, but I've found that if you mimic the drop functionality with your existing files programmatically, you can achieve a similar result of 'reprocessing existing files'.

这是一个不太理想的解决方案,但我发现如果您以编程方式模拟现有文件的放置功能,您可以获得类似“重新处理现有文件”的结果。

This involves storing the files to a variable, removing the files from the Dropzone object and adding the file(s) back in. From your javascript file do:

这涉及将文件存储到变量中,从 Dropzone 对象中删除文件并将文件重新添加。从您的 javascript 文件中执行以下操作:

// Single file example. For multiple use dropzone.addFiles(dropzone.files);
var singleFile = dropzone.files[0];
if (singleFile) {
    dropzone.removeAllFiles(false);
    dropzone.addFile(singleFile);
}