2016-03-08 82 views
0

我有这样的例子:如何删除现有的文件dropzone?

link

代码HTML:

<div class="dropzone dz-clickable" id="myDrop"> 
    <div class="dz-default dz-message" data-dz-message=""> 
      <span>Upload or drag patient photo here</span> 
    </div> 
</div> 

CODE JS:

Dropzone.autoDiscover = false; 
var myDropzone = new Dropzone("div#myDrop", { 
    addRemoveLinks: true, 
    url: "#", 
    maxFiles: 1, 
    init: function() { 

    this.on("maxfilesexceeded", function(file) { 
     alert("You are not allowed to chose more than 1 file!"); 
     this.removeFile(file); 

    }); 

    this.on("addedfile", function(file) { 
     myDropzone.options.removefile.call(myDropzone, mockFile); 
     // I want to delete an existing element 
    }); 

    } 
}); 

var fileName = $('#profilePicture').val(); 
var mockFile = { 
    name: fileName, 
    size: 12345 
}; 
myDropzone.options.addedfile.call(myDropzone, mockFile); 
myDropzone.options.thumbnail.call(myDropzone, mockFile, "https://lh3.googleusercontent.com/-eubcS91wUNg/AAAAAAAAAAI/AAAAAAAAAL0/iE1Hduvbbqc/photo.jpg?sz=104"); 

我想要做的是用户上传的时候一个文件,然后现有的一个是重新移动。

你如何正确地做到这一点?

在此先感谢!

+0

在上传另一个文件之前,'myDropzone.removeAllFiles()'怎么样? – wong2

+0

你能编辑我的例子吗? –

+0

没人知道..? –

回答

4

这是一个工作方法:

var currentFile = null; 
Dropzone.autoDiscover = false; 
var myDropzone = new Dropzone("div#myDrop", { 
    addRemoveLinks: true, 
    url: "#", 
    maxFiles:1, 
    init: function() { 
    this.on("addedfile", function(file) { 
     if (currentFile) { 
     this.removeFile(currentFile); 
     } 
     currentFile = file; 
    }); 
    } 
}); 
+0

是的,但我想要替换第一个图像(默认加载) –

+0

@ D.Cristi然后在文件底部添加'currentFile = mockFile;' – wong2

+0

@ D.Cristi这里是一个工作示例:http:/ /codepen.io/anon/pen/qZbvwV – wong2

0

工作对我来说,如果图像是在悬浮窗已上载,它不会让我增加更多。

this.on("addedfile", function(file) { 
    /* Valid only in the dropzone . If a repetitive document shows ALERT and the previous item will disappear.(Sorry my English). */ 
    if (this.files.length) { 
    var i, len, pre; 
    for (i = 0, len = this.files.length; i < len - 1; i++) { 
     if (this.files[i].name == file.name && this.files[i].size == file.size && this.files[i].lastModifiedDate.toString() == file.lastModifiedDate.toString()) { 
     alert("Doc: " + file.name + " is already registered.") 
     return (pre = file.previewElement) != null ? pre.parentNode.removeChild(file.previewElement) : void 0; 
     } 
    } 
    } 
}); 
1

试试这种方法。

removedfile: function(file) { 
      file.previewElement.remove(); 
} 
0

“每次你的活动” 我爱:成功 - 错误 - 发送 - 从这项功能可以做到这一点的删除文件removedfile或addedfile等

http://www.dropzonejs.com/#event-list

init : function() { 
    self.on('Every your events', function (file, response) { 
     this.removeFile(file); 
    } 
} 

Dropzone.forElement("div#myDrop").removeAllFiles(true); 
1

试试这种方法:

success: function (file, response) { 
        if (this.files.length > 1) 
         this.removeFile(this.files[0]); 
        //Do others tasks... 
       } 

使用这种方法,前一项将被删除。