将ID添加到Dropzone.js中的预览div

时间:2015-01-04 11:56:40

标签: javascript jquery html dropzone.js

我试图为Dropzone.js中上传的每个文件添加一个id属性,所以我稍后可以对它进行排序。

这是我的代码:

Dropzone.options.pictureDropzone = {
  paramName: "file",
  addRemoveLinks: true,
  init: function() {
    this.on("success", function(file, response) {
        file.serverId = response.id;
        $(file.previewTemplate).find('.dz-preview').attr('id', "document-" + file.serverId);
    });
  }
};




该行

$(file.previewTemplate).find('.dz-preview').attr('id', "document-" + file.serverId);

应该添加id,但它什么都不做。 也用prop()尝试了。

如果我选择不同的元素,它确实可以正常工作。例如,这适用于.dz-details

$(file.previewTemplate).find('.dz-details').attr('id', "document-" + file.serverId);

但我似乎无法找到将其添加到dz-preview元素的方法。


HTML结构如下所示:

<div class="dz-preview dz-processing dz-image-preview dz-success">
    <div class="dz-details"> ... </div>
    <div class="dz-progress"> ... </div>
    <div class="dz-success-mark"> ... </div>
</div>



谢谢你的帮助:)

5 个答案:

答案 0 :(得分:19)

我知道这已经过时了,但如果有人还在寻找答案: -

      this.on("success", function(file, response) {
          file.previewElement.id = response.id;
      });

答案 1 :(得分:3)

this.on("success", function(file, response) {
    file.serverId = response.id;
    $(".dz-preview:last-child").attr('id', "document-" + file.serverId);
});

答案 2 :(得分:1)

我有类似的问题,但是通过在javascript中声明变量来尝试它,以下是代码:

$("#fileUpload${dropdown}").dropzone(
                {

                    url : "uploadAdditionalFile?name="+$("#fileUpload${dropdown} div:first-child").prop('id'),
                    addRemoveLinks : true,
                    maxFiles : 1,
                    init : function() {
                        var imageId = $("#fileUpload${dropdown} div:first-child").prop('id');
                        this.on("maxfilesexceeded",
                            function(file) {
                                    alert("Only one file can be uploaded at a time");
                                    this.removeFile(file);
                                        });
                                this.on("addedfile",
                                        function(file) {
                                            switch (file.type) {
                                            case 'application/pdf':
                                                this.emit("thumbnail",file,"/sbms/static/img/pdf.png");
                                                break;
                                            case 'application/msword':
                                                this.emit("thumbnail",file,"/sbms/static/img/word.png");
                                                break;
                                            }
                                        }
                                );
                                 this.on('removedfile', function(file){
                                     $.ajax({
                                            type : "GET",
                                            url : "removeAdditionalMediaPreviewForm?id="+imageId,
                                            dataType : "json",
                                            async : false,
                                            success : function(response) {
                                                if (response.status == 'SUCCESS') {
                                                    alert("File removed successfully.")
                                                }else {
                                                    alert("File not removed successfully.")
                                                }
                                            }
                                        });
                                }); 

                    },

                    success : function(file,response) {
                        var imgName = response;
                        file.previewElement.classList.add("dz-success");
                        console.log("Successfully uploaded :"+ imgName);
                    },
                    error : function(file,response, xhr) {
                        alert("Unable to upload file. Please check if it is a valid doc or pdf file.");
                        this.removeFile(file);
                    }
                });

imageId是一个存储id的变量,稍后在文件删除时使用。

答案 3 :(得分:1)

previewElement转换为jQuery对象并执行任何操作。

   this.on("success", function(file, response) {
      $(file.previewElement).attr("id", response.id);
  });

答案 4 :(得分:0)

<块引用>

如果用户删除多个文件,这将严重失败。(Szczepan Hołyszewski 2015 年 12 月 17 日 18:45);

如果您设置选项 uploadMultiple: false,BryanFong 的回答不会失败。默认情况下是这样设置的。在这种情况下,Dropzone 为每个文件向服务器发送单独的请求。因此,每个单独的文件都会触发“成功”事件。

如果 uploadMultiple: true。 Dropzone 将向服务器发送所有文件的单个请求。而“成功”事件将触发一次。以下代码将处理该问题。

    YourDropzoneInstance.on("success", function(file, response) {
        response.files.forEach(file) {
            file.previewTemplate.id = file.id;
        }
    }

同样,您需要从服务器文件数组返回。 在 PHP 中它看起来像

    function yourFileUploadHandler() {
        ...
        // your server file upload implementation        

        $response = [
            "files" => [
                ["id" = 1, ...],
                ["id" = 2, ...],
                ...
             ],
        ];
    
        return json_decode($response);
    }