目前,我使用下拉区域来处理Jquery中的文件上传。到目前为止还不错。
唯一的问题是,在配置中有一个maxFileSize选项,它限制了“单一”文件的大小。
由于服务器(php.ini)也有“总”文件大小的限制,我想知道如何在dropzone.js中限制它?
非常感谢。
发布于 2015-06-30 02:59:14
我只看到maxfilesize,paralleluploads和maxfiles
我认为您可能需要在添加文件时跟踪它们的大小,可能需要使用
this.on("addedfile", function(file) { // perform task // });..。记录文件大小,如果超过总文件大小,则禁用“提交”按钮。
document.getElementById('dropsubmit').disabled = false;在添加和删除文件时,可以使用"addedFile“和"removedFile”事件来更改跟踪文件大小的变量。根据累积大小,将submit按钮设置为true或false。
发布于 2019-04-02 09:26:24
我找到了定义accept函数的解决办法:
var totalsize = 0.0;
$("div#dropzone").dropzone({
accept: function(file, done) {
if (totalsize >= MAX_TOTAL_SIZE) {
file.status = Dropzone.CANCELED;
this._errorProcessing([file], "Max limit reached", null);
}else {
done();
}
}
init: function() {
this.on("addedfile", function(file) {
totalsize += parseFloat((file.size / (1024*1024)).toFixed(2));
});
this.on("removedfile", function(file) {
if(file.upload.progress != 0){
totalsize -= parseFloat((file.size / (1024*1024)).toFixed(2));
}
});
this.on("error", function(file) {
totalsize -= parseFloat((file.size / (1024*1024)).toFixed(2));
});
}
}); 如果MAX_TOTAL_SIZE是该下拉区域的最大值,则该区域不应超过。
发布于 2017-09-15 19:02:22
现在有点晚了,但也许其他人需要这个。您需要在init函数中创建一个新变量"totalSize“。将事件侦听器添加到fileAdd以增加大小,并添加另一个事件侦听器以进行子结构,然后在发送显示错误的请求之前进行一点控制,我对英语很差,下面是一个示例:
...
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');
}
});
}也许没有那么清楚,所以有一个完整的代码示例,在我的代码中我添加了一个样式化的remove按钮,所以不要忘记删除它:
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');
}
});
} https://stackoverflow.com/questions/31128505
复制相似问题