Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何删除现有的文件dropzone?_Javascript_Jquery_Html_Dropzone.js - Fatal编程技术网

Javascript 如何删除现有的文件dropzone?

Javascript 如何删除现有的文件dropzone?,javascript,jquery,html,dropzone.js,Javascript,Jquery,Html,Dropzone.js,我有这个样本: 代码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> Dropzone.au

我有这个样本:

代码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>
 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");
我想做的是当用户上传一个文件时,然后删除现有的文件

如何正确地执行此操作


提前谢谢

这是一种有效的方法:

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;
    });
  }   
});
 success: function (file, response) {
                    if (this.files.length > 1)
                        this.removeFile(this.files[0]);
                    //Do others tasks...
                }

对我来说,如果图像已经上传到dropzone,它不允许我添加更多

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;
       }
     }
   }
 });
this.on(“addedfile”,函数(文件){
/*仅在dropzone中有效。如果重复文档显示警报,则上一项将消失。(对不起,我的英语)*/
if(this.files.length){
var i,len,pre;
对于(i=0,len=this.files.length;i
试试这个方法

removedfile: function(file) {
            file.previewElement.remove();
}
“每个你的事件”如:成功-错误-发送-删除文件或添加文件等

要从中删除文件,请退出此功能,可以执行以下操作:

Dropzone.forElement("div#myDrop").removeAllFiles(true);
尝试以下方法:

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;
    });
  }   
});
 success: function (file, response) {
                    if (this.files.length > 1)
                        this.removeFile(this.files[0]);
                    //Do others tasks...
                }

使用此方法,将删除前一项

您好,您只需将
addRemoveLinks:true,
添加到dropzone函数,然后 在dropzon ajax之后添加成功函数

success:函数(文件、响应){
file.serverId=response.id;
$(“.dz preview:last.dz remove”).attr('onclick','delete_file('+file.serverId+'));
}
在这段代码中,成功上传文件后,在onclick中从dropzone向最后添加一个标记以调用delete_file函数,在这个函数中,您可以接收id并将id发送到后端,查找文件
在后端并删除文件

上传另一个文件之前,如何
myDropzone.removeAllFiles()
请编辑我的示例?是的,但我想替换第一个图像(默认已加载)@D.Cristi,然后只需添加
currentFile=mockFile位于文件底部