dropzone js用删除按钮链接删除网址

时间:2014-06-16 07:03:03

标签: javascript jquery dropzone.js

在Dropzonejs中,我创建了删除按钮,然后将其附加到缩略图,如何使用addRemoveLinks:true直接将我从服务器进行geeting的网址链接到删除按钮,

//Write function if you need to add some event after files added
      myDropzone.on("addedfile", function(file) {
        console.log('Files Added using ---------->'+$attrs.apiCall);
      var _this=this;
        /* Maybe display some more file information on your page */
       var removeButton = Dropzone.createElement("<button data-dz-remove>-Remove file</button>");
        removeButton.addEventListener("click", function(e) {
        e.preventDefault();
        e.stopPropagation();
        _this.removeFile(file);
        });
        file.previewElement.appendChild(removeButton);
      });

3 个答案:

答案 0 :(得分:5)

您可以添加删除链接..在添加的文件事件中,您可以在服务器响应中获取URL并将其设置为删除链接。

 myDropzone.on("addedfile", function (file) {
     var _this = this;

     /* Maybe display some more file information on your page */
        var removeButton = Dropzone.createElement("<button data-dz-remove " +
                        "class='del_thumbnail btn btn-default'><span class='glyphicon glyphicon-trash'></span></button>");

        removeButton.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopPropagation();
        var server_file = $(file.previewTemplate).children('.server_file').text();
        // Do a post request and pass this path and use server-side language to delete the file
        //          $.post(server_file,{'X-CSRFToken': $cookies.csrftoken}, 'json');
        $http({
            method: 'POSt',
            url: server_file,
            headers: {
                   'X-CSRFToken': $cookies.csrftoken
            }
        });
         _this.removeFile(file);
         });
         file.previewElement.appendChild(removeButton);
 });

答案 1 :(得分:5)

这适用于Dropzone 5.0.0

<script>
    Dropzone.options.dropzoneForm = {
        addRemoveLinks: true,
        dictDefaultMessage: "<strong>Drop files here or click to upload. </strong>",
        init: function() {
            this.on("complete", function(file) {
                $(".dz-remove").html("<div><span class='fa fa-trash text-danger' style='font-size: 1.5em'></span></div>");
            });
        }
    };
</script>

答案 2 :(得分:1)

添加

addRemoveLinks: true,

然后在

中使用以下内容
init: function () {}

点击dz-remove后,它会转到它的父级,然后查找图片ID所在的span元素的文本。

使用$ ajax将imageId发送到您的操作并执行您想要的操作。 注意:我在这里使用toastr进行用户通知。

$(".dz-remove").on("click", function (e) {
     e.preventDefault();
     e.stopPropagation();

     var imageId = $(this).parent().find(".dz-filename > span").text();

     $.ajax({
     url: "Your url here",
     data: { imageId: imageId},
     type: 'POST',
     success: function (data) {
          if (data.NotificationType === "Error") {
               toastr.error(data.Message);
          } else {
               toastr.success(data.Message);                          
          }},
          error: function (data) {
               toastr.error(data.Message);
          }
     })

});

希望这可以帮助你兄弟:)