在dropzone .js中的预览模板中为每个图像添加图像ID

时间:2014-07-31 04:25:39

标签: javascript jquery html dropzone.js

我只想为使用dropzone.js上传的每张图片添加图片ID 目前预览模板是这样的

<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 个答案:

答案 0 :(得分:5)

此代码从服务器获取所有图像并初始化dropzone容器。 我希望这能帮到你

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