【问题标题】:Multiple dropzone.js - single page多个 dropzone.js - 单页
【发布时间】:2013-11-13 04:10:41
【问题描述】:

而不是在单个 dropzone 元素上上传多个文件 - 是否可以在单个页面上拥有多个 dropzone 元素?

当有多个元素时,似乎 dropzone 在选择对话框之后甚至都没有触发,每个元素都有自己的 dropzone 初始化

【问题讨论】:

    标签: javascript jquery dropzone.js


    【解决方案1】:

    使用 dropzone 的典型方式是创建一个带有 dropzone 类的表单元素:

    <form action="/file-upload"
          class="dropzone"
          id="my-awesome-dropzone"></form>
    

    就是这样。 Dropzone 将查找所有具有类 dropzone 的表单元素,自动将自己附加到它,并将放入其中的文件上传到指定的操作属性。然后,您可以像这样访问 dropzone 元素:

    // "myAwesomeDropzone" is the camelized version of the HTML element's ID
    Dropzone.options.myAwesomeDropzone = {
      paramName: "file", // The name that will be used to transfer the file
      maxFilesize: 2, // MB
      accept: function(file, done) {
        if (file.name == "justinbieber.jpg") {
          done("Naha, you don't.");
        }
        else { done(); }
      }
    };
    

    【讨论】:

      【解决方案2】:

      据我所知,您可以创建自己的 dropzone ,然后可以有多个 dropzone 元素。

      // Dropzone class:
      var myDropzone = new Dropzone("div#myId", { url: "/file/post"});
      
      // jQuery
      $("div#myId").dropzone({ url: "/file/post" });
      

      【讨论】:

        【解决方案3】:

        是的,您可以在一个页面上拥有无限数量的放置区。

        例子:

        <form action="/controller/action">
            <div class="dropzoner" id="dropzone1"></div>
            <div class="dropzoner" id="dropzone2"></div>
        </form>
        
        <script>
        Dropzone.autoDiscover = false; // Very important
        
        InitializeDropzones();
        
        function InitializeDropzones() { // You only need to encapsulate this in a function if you are going to recall it later after an ajax post.
            Array.prototype.slice.call(document.querySelectorAll('.dropzoner'))
                .forEach(element => {
        
                if (element.dropzone) {
                    element.dropzone.destroy();
                } // This is only necessary if you are going to use ajax to reload dropzones. Without this, you will get a "Dropzone already exists" error.
        
                var myDropzone = new Dropzone(element, {
                    url: "/controller/action",
                    headers: {
                        "headerproperty": value,
                        "headerproperty2": value2
                    },
                });
            })
        }
        </script>
        

        在处理多个放置区时可能会节省一些时间的一些注意事项:

        通过 ajax 重新加载任何附加了 dropzone 的元素后,您需要在元素上重新初始化该 dropzone。

        例如:

        myDropzone.on("success", function (response) {
                 toastr[show a toastr];
                 $("#ContainerThatHasDropzones").load("/controller/action/" + id, function() {
                 Dropzone.discover(); // This is very important. The dropzones will not work without this after reloading via ajax.
                 InitializeDropzones();
        });
        

        【讨论】:

          猜你喜欢
          • 2019-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-23
          • 2016-04-21
          • 2015-09-13
          相关资源
          最近更新 更多