Dropzone - max文件不工作

Dropzone - max files not working

本文关键字:工作 文件 max Dropzone      更新时间:2023-09-26

我试过将上传文件的限制设置为只有一个。我已经尝试了前面问题中的所有建议,但对我来说都不起作用。每次我都能上传多个文件,想上传多少就上传多少。

这是我的尝试之一:

var token = "{{ csrf_token() }}";
Dropzone.autoDiscover = false;
 var myDropzone = new Dropzone("div#dropzoneFileUpload", {
     url: "/admin/upload",
     params: {
        _token: token
      }
 });
 Dropzone.options.myAwesomeDropzone = {
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 2, // MB
    addRemoveLinks: true,
    maxFiles: 1,
    init: function() {
      this.on("maxfilesexceeded", function() {
        if (this.files[1]!=null){
          this.removeFile(this.files[0]);
        }
      });
    },
    accept: function(file, done) {
    }
  };

我是这样调用脚本的:

<script src="{{ asset('js/dropzone/dropzone.js') }}"></script>
<script src="{{ asset('js/image-upload.js') }}"></script>

将dropzone配置拆分为两个不同的方法。并且只有第一个被使用——包含url选项的那个,第二个包含maxFiles选项的那个被忽略。

您必须在第一个方法中包含所有配置,以编程方式创建dropzone,如下所示:

Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
    url: "/admin/upload",
    params: {
       _token: token
    },
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 2, // MB
    addRemoveLinks: true,
    maxFiles: 1,
    init: function() {
      this.on("maxfilesexceeded", function() {
        if (this.files[1]!=null){
          this.removeFile(this.files[0]);
        }
      });
    },
    accept: function(file, done) {
    }
 });

或者使用dropzone自动发现功能的第二种方法,如果您的dropzone元素的id为#dropzoneFileUpload,则这样做:

 Dropzone.options.dropzoneFileUpload = {
    url: "/admin/upload",
    params: {
       _token: token
    },
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 2, // MB
    addRemoveLinks: true,
    maxFiles: 1,
    init: function() {
      this.on("maxfilesexceeded", function() {
        if (this.files[1]!=null){
          this.removeFile(this.files[0]);
        }
      });
    },
    accept: function(file, done) {
    }
};