Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Dropzone inside a Modal box

I have a form inside a modal box of a bootstrap theme. The form contains few input fields and a Dropzone field, all of which sends the data across to the server through ajax. After filling the form, I, obviously need to reset the form for next set of inputs. I have managed to successfully reset the form fields, EXCEPT the Dropzone image.

So now when I click submit, after filling the form, the data is entered. On clicking the button to open the modal again to add a new set, the previous image is still shown there. I need that also to be gone.

I kept searching for a solution on S.O and many other places. I couldn't get a solution. Any guidance is appreciated.


HTML:

<form enctype="multipart/form-data" id="addProductImageForm" class="dropzone dz-clickable" action="#">
  <div class="dz-default dz-message" ><span>Click here to upload Product Image</span></div>
</form>

JS:

Dropzone.options.addProductImageForm = {
paramName: "productImage", 
maxFilesize: 2,
maxFiles: 1,
uploadMultiple: false,
acceptedFiles: "image/*", 
dictDefaultMessage: "Click to Upload",
addRemoveLinks: true,
accept: function(file, done) { 
    if (file.name == "bg.png") {
        done("Naha, you don't.");
    }
    else {
        done();
    }
},
success: function(d, response, test) {
    var value = response.replace("int(0)", "");
    var value2 = value.replace(/(\r\n|\n|\r)/gm, "");
    $('#product_imageName').val(value2);
},
init: function() {
    this.on("maxfilesexceeded", function(file) {
        alert("No more files please!");
    });
}
};
$("#addProductImageForm").dropzone({url: "/path/to/upload_product_image.php"});

.
.
.
.

var formData = {
  (input values here)
};

$.ajax({
    type: 'POST', 
    url: 'path/to/projectsAction.php',
    data: formData,
    encode: true
})
        .done(function(data) {
            var value3 = data.replace(/(\r\n|\n|\r)/gm, "");
            $('#project_productId').append('<option value="' + value3 + '" selected="selected">' + formData.product_name + '</option>');
            $("#addProductForm").find("input[type=text], textarea, select").val("");
            $("#addProductImageForm").html("");// THIS IS THE LINE THAT REMOVES THE THUMBNAIL. (From the answer given by xrcwrn)
            $('.close').click();
        });

event.preventDefault();
like image 341
Anagh Avatar asked Mar 08 '26 08:03

Anagh


1 Answers

If you are using dropzone like

   <form action="/file-upload"
    class="dropzone"
  id="my-awesome-dropzone"></form>

after posting your data reset your dropzone container like

  $("#my-awesome-dropzone").html("");
like image 172
xrcwrn Avatar answered Mar 10 '26 08:03

xrcwrn