简体   繁体   English

在 Dropzone.js 中为预览 div 添加 ID

[英]Add ID to the preview div in Dropzone.js

I'm trying to add an id attribute to each file uploaded in Dropzone.js, So I can sort it later on.我正在尝试为 Dropzone.js 中上传的每个文件添加一个 id 属性,以便稍后对其进行排序。

This is my code:这是我的代码:

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);
    });
  }
};




The line线

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

Should add the id, but it does nothing.应该添加 id,但它什么也不做。 Tried it with prop() too.也用 prop() 试过了。

If I choose a different element, it does work fine.如果我选择不同的元素,它确实可以正常工作。 for example, this works for .dz-details例如,这适用于 .dz-details

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

But I cannot seem to find a way to add it to the dz-preview element.但我似乎找不到将它添加到 dz-preview 元素的方法。


The HTML structure looks like that: 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>



Thank you for the help :)感谢您的帮助 :)

I know this is old but if anyone is still looking for the answer: -我知道这很旧,但如果有人仍在寻找答案:-

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

Cast the previewElement into jQuery object and perform any action.previewElement转换为 jQuery 对象并执行任何操作。

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

I had similar problem but tried it through declaring a variable in javascript ,following is code :我有类似的问题,但通过在 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 is a variable which stores the id and is used later on while file remove. imageId 是一个存储 id 的变量,稍后在删除文件时使用。

This will fail spectacularly if the user drops multiple files.(Szczepan Hołyszewski Dec 17 '15 at 18:45);如果用户删除多个文件,这将严重失败。(Szczepan Hołyszewski 2015 年 12 月 17 日 18:45);

BryanFoong answer won't fail if you set the option uploadMultiple: false .如果您设置选项uploadMultiple: false BryanFoong 的回答不会失败。 Which is set so by default.默认情况下是这样设置的。 In this case Dropzone sends separate request to the server for each file.在这种情况下,Dropzone 为每个文件向服务器发送单独的请求。 Therefore "success" event triggers for each individual file.因此,每个单独的文件都会触发“成功”事件。

In case the uploadMultiple: true .如果uploadMultiple: true Dropzone will send single request to server for all files. Dropzone 将向服务器发送所有文件的单个请求。 And "success" event will trigger once.而“成功”事件将触发一次。 And following code will handle that.下面的代码将处理它。

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

Again you need to return from server array of files.同样,您需要从服务器文件数组返回。 In PHP it will look like在 PHP 中它看起来像

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM