我正在尝试为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.serverId = response.id;
$(".dz-preview:last-child").attr('id', "document-" + file.serverId);
});
我知道这是旧的,但如果有人仍在寻找答案: -
this.on("success", function(file, response) {
file.previewElement.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的变量,稍后在文件删除时使用。
将previewElement
转换为jQuery对象并执行任何操作。
this.on("success", function(file, response) {
$(file.previewElement).attr("id", response.id);
});