Немного поздно, но, возможно, кому-то это нужно. Ну, вам нужно создать новую переменную «totalSize» в функции init. Добавить listenner события в fileAdd для увеличения размера и еще один, чтобы substruct, потом немного Контрола, прежде чем отправить запрос, чтобы показать ошибку, я плохо с английским, так вот пример:
...
init: function() {
var totalsize = 0.0;
...
this.on("addedfile", function(file) {
...
// increment total size when we add a file (in Mb)
totalsize += parseFloat((file.size/(1024*1024)).toFixed(2));
//substruct the size of the removed file
removeButton.addEventListener("click", function(e) {
...
_this.removeFile(file);
totalsize -= parseFloat((file.size/(1024*1024)).toFixed(2));
...
});
...
});
//and an event for the submission to controle before submit don't forget to prevent default
this.on("sendingmultiple", function(data, xhr, formData) {
if (totalsize <= 20) {
//do the request
}else { // else show the error
$('#error').show();
$('#error').text('Oooops ! total files size must be less then 20Mb');
}
});
}
не может быть, это не так ясно, так что полный пример кода, в моем коде я хочу добавить стилизованную кнопку удаления, так что не забудьте удалить его:
init: function() {
var totalsize = 0.0;
dzClosure = this; // Makes sure that 'this' is understood inside the functions below.
// for Dropzone to process the queue (instead of default form behavior):
document.getElementById("submit-all").addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
dzClosure.processQueue();
});
this.on("addedfile", function(file) {
// Create the remove button
var removeButton = Dropzone.createElement("<a href='javascript:;'' class='btn red btn-sm btn-block'>Remove</a>");
// 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);
totalsize -= parseFloat((file.size/(1024*1024)).toFixed(2));
// If you want to the delete the file on the server as well,
// you can do the AJAX request here.
});
// Add the button to the file preview element.
file.previewElement.appendChild(removeButton);
//increment
totalsize += parseFloat((file.size/(1024*1024)).toFixed(2));
});
//send all the form data along with the files:
this.on("sendingmultiple", function(data, xhr, formData) {
if (totalsize <= 20) {
console.log(totalsize);
//u can append formData here too
formData.append("something", jQuery("#something").val());
}else {
$('#error').show();
$('#error').text('Oooops ! total files size must be less then 20Mb');
}
});
}
Вы можете контролировать максимальный размер загрузки файла на файл , а также контролирует максимальное количество загруженных файлов. Это то, что вы ищете? –