繁体   English   中英

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

[英]Add ID to the preview div in 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>



感谢您的帮助 :)

我知道这很旧,但如果有人仍在寻找答案:-

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

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

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

我有类似的问题,但通过在 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 的变量,稍后在删除文件时使用。

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

如果您设置选项uploadMultiple: false BryanFoong 的回答不会失败。 默认情况下是这样设置的。 在这种情况下,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);
    }

暂无
暂无

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

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