简体   繁体   English

如何从 Dropzone 的下拉框区域删除文件?

[英]How to remove a file from the drop box area of Dropzone?

I use the Dropzone.js for uploading the files in an application.After a user selects the pictures from his local computer's file manager to the dropzone area,if user later finds that he/she want to remove one of the picked pictures or files.How to achieve it?我使用 Dropzone.js 在应用程序中上传文件。当用户从本地计算机的文件管理器中选择图片到 dropzone 区域后,如果用户后来发现他/她想要删除其中一张选中的图片或文件。如何实现?

HTML code: HTML 代码:

 <form id="file-up" action="upload_file.php" method="post" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">

 <div class="fallback">

 <input type="file" id="upload_file" name="upload_file[]"  multiple/> 
 <input type='hidden' id='uploadvalues' />

 </div>

 </form>

The jquery jquery

   <script type="text/javascript">

    Dropzone.options.myAwesomeDropzone = {
      maxFilesize: 20, // Size in MB
      addRemoveLinks: true,
        removedfile: function(file) { 
          var fileRef;
          return (fileRef = file.previewElement) != null ? 
                  fileRef.parentNode.removeChild(file.previewElement) : void 0;
        },

   success: function(file, response) {
              alert(response);
            },

   error: function(file, response) {
                  alert(response);
            }

 };




  </script>

PHP server side code for uploading PHP 用于上传的服务器端代码

     <?php
      if(isset($_POST['submit_image'])){
     $ds          = DIRECTORY_SEPARATOR;  //1

     $storeFolder = 'uploads';   //2

     if (!empty($_FILES)) {

     $tempFile = $_FILES['file']['tmp_name'];          //3             

     $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

     $targetFile =  $targetPath. $_FILES['file']['name'];  //5

     move_uploaded_file($tempFile,$targetFile); //6

       }
     }
    ?>

You can also use the following snippet:您还可以使用以下代码段:

Dropzone.autoDiscover = false;
$(".dropzone").dropzone({
 addRemoveLinks: true,
 removedfile: function(file) {
   var name = file.name; 
   
   $.ajax({
     type: 'POST',
     url: 'upload.php',
     data: {name: name,request: 2},
     sucess: function(data){
        console.log('success: ' + data);
     }
   });
   var _ref;
    return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
 }
})

You can create a delete.php file like such. 您可以像这样创建一个delete.php文件。

</p>
include 'db.php';
$upload_dir = 'myuploads';
$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;
unlink($targetPath.$_GET['fid']);
$obj=new DB();
$sql = "DELETE FROM file_upload WHERE f_name='".$_GET['fid']."'";
$retval = mysqli_query($obj->connection(),$sql);
print_r("Successfully deleted.");
<p style="text-align: justify;">

Updated from Comment: If I understand you right, you want the X to delete the file in dropzone. 从评论更新:如果我理解正确,您希望X删除dropzone中的文件。

removedfile: function(file) {
    x = confirm('Do you want to delete?');
    if(!x)  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM