emit("success") is not being emitted and invoked
Hi,
I'm having problems loading existing files from the server.
Actually I managed to load the files but I can't emit a call to success event it simply does not work. The below code (the emit("success") does not work, the function is never called)
In my success function I dynamically add two buttons one for removal of the image and one for full size image (which is just a href leading to a new page with the full size image) so when I load existing files from the server I want the same two buttons because the user can delete the image and see a full size version of it any time...
This is my code:
Dropzone.options.producerUploadForm = {
dictDefaultMessage: "Click here to add a new image",
acceptedFiles: "image/*",
thumbnailWidth: 140,
thumbnailHeight: 140,
init: function () {
//--Load existing files if any
var existingDocsRaw = @Html.Raw(Model.ImagesJSON);
var existingDocs = [];
var myDropzone = this;
if(existingDocsRaw.length > 0 ) {
for(var i = 0; i < existingDocsRaw.length; i++)
existingDocs.push({
accepted: true,
name: existingDocsRaw[i].FileName,
size: existingDocsRaw[i].Size,
url: existingDocsRaw[i].URL,
status: Dropzone.ADDED
});
}
if(existingDocs.length > 0) {
for(var i = 0; i < existingDocs.length; i++) {
// Create the mock file:
var mockFile = existingDocs[i];
// Call the default addedfile event handler
myDropzone.emit("addedfile", mockFile);
// And optionally show the thumbnail of the file:
myDropzone.emit("thumbnail", mockFile, mockFile.url);
// Make sure that there is no progress bar, etc...
myDropzone.emit("complete", mockFile);
// Invoke success event to add remove button
myDropzone.emit("success", { File: { FileName: mockFile.name, URL: mockFile.url }}, 'abc');
myDropzone.files.push(mockFile);
}
}
this.on("sending", function(file, xhr, formData) {
var token = $('[name=__RequestVerificationToken]').val();
formData.append("uid", '@Model.UID');
formData.append("producerId", @Model.ProducerID);
formData.append("__RequestVerificationToken", token);
});
this.on("success",function(file,args) {
// Create the remove button
var removeFileButton = '<a class="fa fa-times remove-btn pull-left" href="' + '@Url.Action("DeleteProducerImage","Upload")' + '"></a>';
var fullSizeButton = '<a target="_blank" class="fa fa-th-large remove-btn pull-right" href="' + args.File.URL + '"></a>';
var removeButton = Dropzone.createElement(removeFileButton);
var fullSizeButton = Dropzone.createElement(fullSizeButton);
// Capture the Dropzone instance as closure.
var _this = this;
// Listen to the click event
removeButton.addEventListener("click", function (e) {
// Make sure the button click doesn't submit the form:
e.preventDefault();
e.stopPropagation();
// Remove the file preview.
_this.removeFile(file);
// If you want to the delete the file on the server as well,
// you can do the AJAX request here.
$.ajax({
method:'post',
url: $(this).attr('href'),
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: { uid: '@Model.UID', producerId: '@Model.ProducerID', fileName: args.File.FileName }
}).done(function() { });
});
// Add the button to the file preview element.
file.previewElement.appendChild(removeButton);
file.previewElement.appendChild(fullSizeButton);
});
}
};
Please if you can assist me I would be very grateful.
Thanks in advance!
Hi, Missing quotation marks
this.on("sending", function(file, xhr, formData) {
var token = $('[name=__RequestVerificationToken]').val();
formData.append("uid", '@Model.UID');
formData.append("producerId", @Model.ProducerID);
formData.append("__RequestVerificationToken", token);
});
---->
formData.append("producerId", '@Model.ProducerID');
Hi,
Thank you for answering, and reminding me that I posted this question here.
No the quotation marks are ok, the solution was a lot more trivial (I call this syndrome the "forest syndrome" cuz you can't see the forest from the trees or vice-versa):
THIS:
this.on("sending", function(file, xhr, formData) {
var token = $('[name=__RequestVerificationToken]').val();
formData.append("uid", '@Model.UID');
formData.append("producerId", @Model.ProducerID);
formData.append("__RequestVerificationToken", token);
});
this.on("success",function(file,args) {
// Create the remove button
var removeFileButton = '<a class="fa fa-times remove-btn pull-left" href="' + '@Url.Action("DeleteProducerImage","Upload")' + '"></a>';
var fullSizeButton = '<a target="_blank" class="fa fa-th-large remove-btn pull-right" href="' + args.File.URL + '"></a>';
var removeButton = Dropzone.createElement(removeFileButton);
var fullSizeButton = Dropzone.createElement(fullSizeButton);
// Capture the Dropzone instance as closure.
var _this = this;
// Listen to the click event
removeButton.addEventListener("click", function (e) {
// Make sure the button click doesn't submit the form:
e.preventDefault();
e.stopPropagation();
// Remove the file preview.
_this.removeFile(file);
// If you want to the delete the file on the server as well,
// you can do the AJAX request here.
$.ajax({
method:'post',
url: $(this).attr('href'),
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: { uid: '@Model.UID', producerId: '@Model.ProducerID', fileName: args.File.FileName }
}).done(function() { });
});
has to be defined before everything else at the start of the init: function and not as it is now at the end, cuz the code that adds existing files is before this event handler and thus when the line "myDropzone.emit("success", { File: { FileName: mockFile.name, URL: mockFile.url }}, 'abc');" is invoked, only core dropzone success function is called and not my custom handler, which of course totally makes sense since from that line the custom success handler does not exist yet :D
I'm closing this issue now and leaving it for future reference if somebody has a similar problem.
Thank you!
This example perfectly fixed my issue. Thank you !
here is my code snippet, hope to be helpful to others..
// 1. put "success" event on the top. eg. I wanna change the progress bar from "progress-striped active" to "progress" once it ups to 100%
myDropzone.on("success", function(file, responseObject) {
if (file.previewElement && file.previewElement.childNodes.length > 1 && file.previewElement.childNodes[1].nodeName.toLowerCase() === 'div' &&
file.previewElement.childNodes[1].lastElementChild.nodeName.toLowerCase() === 'div') {
file.previewElement.childNodes[1].lastElementChild.className = "progress";
}
});
//2. then call emit. the order is important.
addExistingFiles();
function addExistingFiles(){
var mockFile = { name: "Filename a.pdf", size: 12345678 };
myDropzone.emit("addedfile", mockFile);
myDropzone.emit("success", mockFile);
myDropzone.files.push(mockFile);
}