3

I use dropzone.js for a nice upload form. I linked the php code to upload the files and i setted addRemoveLinks=true so i have the remove button.

I need an idea how to efectivly delete the filse uploaded with a php code when i hit remove button.

The php is simple to do but i need t know how to relate them. I already tryed using $.post in this function removedfile: function(file) but no succes.

  removedfile: function(file) {
    $.post("test.php");
    var _ref;
    return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;

},
Magnus
  • 391
  • 1
  • 7
  • 35

1 Answers1

19

First off, you shouldn't simply overwrite the default removedfile event handler, but rather register your own handler along with it.

You need to first get the ID back from the server (so you know how to relate to it) and then use this to setup the delete call.

Dropzone.options.myDropzone = {
  init: function() {
    this.on("success", function(file, response) {
      file.serverId = response; // If you just return the ID when storing the file
      // You can also return a JSON object then the line would
      // look something like this:
      //
      // file.serverId = response.id;
      //
      // In that case make sure that you respond with the mime type
      // application/json
    });
    this.on("removedfile", function(file) {
      if (!file.serverId) { return; } // The file hasn't been uploaded
      $.post("delete-file.php?id=" + file.serverId); // Send the file id along
    });
  }
enyo
  • 16,269
  • 9
  • 56
  • 73
  • Isnt the "File" readonly? I tried this aswell but at the removefile callback that file handler is still the original one without that other data? – Dirkos Feb 06 '14 at 10:45
  • i am trying to use the above technique to first show and then remove files already uploaded on server but the on success event is not firing. Please help – Abhishek Kumar Jun 24 '14 at 12:13