Adding image id to each image in preview template

2019-07-02 03:56发布

问题:

I just want to add image id to each image that are uploaded using dropzone.js currently preview template is like this

<div class="dz-preview dz-image-preview" id="1">  
    <div class="dz-details">    
        <div class="dz-filename"><span data-dz-name="">car-menu03.jpg</span></div>
        <div class="dz-size" data-dz-size=""><strong>85.9</strong> KiB</div>
        <img data-dz-thumbnail="" alt="car-menu03.jpg" src="http://server1/akhil/workspace/XENSALE/assets/uploads/images/cars/2/car-menu03.jpg">
    </div>
    <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress=""></span></div>
    <div class="dz-success-mark"><span>✔</span></div>
    <div class="dz-error-mark"><span>✘</span></div>
    <div class="dz-error-message"><span data-dz-errormessage=""></span></div>
    <a class="dz-remove" href="javascript:undefined;" data-dz-remove="">Remove</a>
</div>

回答1:

This code get all images from the server and initialize the dropzone container. I hope this help you

// Initialize dropzone
init: function() {
   thisDropzone = this;
   // Call server to get all images in JSON format {id, filename, imagesize}
   $.get("get-all-images", function(data) {
        $.each(data.data, function(key,value){

           var mockFile = { name: value.filename, size: value.imagesize };
           thisDropzone.options.addedfile.call(thisDropzone, mockFile);
           thisDropzone.options.thumbnail.call(thisDropzone, mockFile, value.file);

           // On the created element added the id property 
           $(mockFile.previewElement).prop('id', value.id);
       });
   });
}