Remove file from file upload javascript

With the Dropzone.js script you can easily implement drag and drop file upload functionality on your website.

But by default, it only allows file uploading with no file removal option.

For enabling the remove link following with the uploaded file preview require to explicitly initialize the dropzone and define the options.

Remove file from file upload javascript


Contents

  1. Download and Include
  2. HTML
  3. CSS
  4. Script
  5. PHP
  6. Demo
  7. Conclusion

1. Download and Include

  • Download dropzone.js library from here.
  • Include dropzone.min.css and dropzone.min.js with the jQuery library at the section.





2. HTML

Create a

and set its action='upload.php'.

Completed Code



 
  Delete the uploaded file from Dropzone.js - PHP
  
  
  

  
  
  
 
 
  

3. CSS

Create style.css file.

Completed Code

.container{
 margin: 0 auto;
 width: 50%;
}

.content{
 padding: 5px;
 margin: 0 auto;
}
.content span{
 width: 250px;
}

.dz-message{
 text-align: center;
 font-size: 28px;
}

4. Script

Explicitly initialize using dropzone() method and for enabling remove file add addRemoveLinks: true and removefile options.

Send AJAX request from the removedfile function where get the file name using the file.name and pass in the AJAX as data.

At the end call –

var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;

to remove the thumbnail from the dropzone container.

Completed Code

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;
 }
});

Create a upload.php file and a upload directory.

  • If $request == 1 then store file to upload directory and
  • If $request == 2 then remove the file from the server according to the $_POST['name'] value.

Completed Code


6. Demo

View Demo


7. Conclusion

Use addRemoveLinks: true option to enable remove link and handle it with removedfile option function by sending AJAX request to delete the file from the server.

If you found this tutorial helpful then don't forget to share.

How do you delete selected files on file upload control?

bind('focus', function() { // Clear any files currently selected in #upload-file $('#upload-file'). val(''); }) ; // Choose uploading new one - this works ok $('#upload-file'). bind('focus', function() { // Clear any files currently selected in #select-file $('#select-file').

How do we remove file from the input file filed?

How to add and delete files in input type="file"?.
in the selection dialog box, select the desired files to download the files, click "Open".
after that, in a certain block, you receive a list of all loaded files with the "delete" button, which when pressed deletes the file (not all!).

How do I delete a single file from input type file multiple?

How do I delete a single file from input type file multiple? Just assign $('input:file#upload')[0]. files to an Array and then remove items from that array using splice or method of your choice and then use that Array .

How do I delete an uploaded file in HTML?

You can't edit them, you can only fully clear the file queue. - It's read only. However, what you can do is push those items into a separate array and apply any changes to it, then proceeding to manually upload them.