如何在dropzone插件中添加removefile选项? [英] how to add removefile option in dropzone plugin?
问题描述
我使用过dropzone.js( http://www.dropzonejs.com/).我正在尝试添加删除按钮以删除图像.但是我收到了javascript undefined错误
I have used dropzone.js (http://www.dropzonejs.com/). I am trying to add delete button to delete the image. but i am getting javascript undefined error
<script>
Dropzone.options.myDropzone = {
init: function() {
addRemoveLinks: true,
thisDropzone = this;
$.get('photo-add.php', function(data) {
$.each(data, function(key,value){
var mockFile = { name: value.name, size: value.size };
thisDropzone.options.addedfile.call(thisDropzone, mockFile);
thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "upload/"+value.name);
});
});
this.on("removedfile", function(file) {
alert(file.name);
console.log(file);
// Create the remove button
var removeButton = Dropzone.createElement("<button>Remove file</button>");
/ / Capture the Dropzone instance as closure.
var _this = this;
// Listen to the click event
removeButton.addEventListener();
// Add the button to the file preview element.
file.previewElement.appendChild(removeButton);
});
}
};
</script>
推荐答案
我认为您没有以正确的方式配置dropzone
.
I think you are not configuring the dropzone
in proper way.
init: function() {
addRemoveLinks: true,
上面的代码无效.
像这样使用
Dropzone.options.myAwesomeDropzone = {
addRemoveLinks: true,
init: function() {
}
...
};
否则您也可以像this.addRemoveLinks = true
如果您要处理删除文件的事件,可以这样使用
If you want to handle the event of removing file you can use like this
removedfile: function(file) {
x = confirm('Do you want to delete?');
if(!x) return false;
}
在服务器端处理文件删除.
在dropzone的成功方法上,将文件名推送到一个数组,例如:file_up_names=[];
on success method of dropzone push the file name to an array eg : file_up_names=[];
success:function(file){
file_up_names.push(file.name);
现在删除时获取名称,并将其传递到php页面以进行文件删除.
now on delete get the name and pass it to the php page for file deletion.
removedfile: function(file) {
x = confirm('Do you want to delete?');
if(!x) return false;
for(var i=0;i<file_up_names.length;++i){
if(file_up_names[i]==file.name) {
$.post('delete_file.php',
{file_name:file_up_names[i]},
function(data,status){
alert('file deleted');
});
}
}
}
还请注意,如果您在服务器端更改了文件名,则必须找回要删除的文件名.
also note if you have changed the file name on server side you have to get back that file name to delete.
这篇关于如何在dropzone插件中添加removefile选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!