[英]How to throw error in dropzone for maxfilesize?
在我的代碼中,我將文件大小設置為 1mb ,它工作正常但沒有拋出任何錯誤消息我該如何設置?
$(document).ready(function () {
$("#my-dropzone").dropzone({
maxFiles: 1,
maxFilesize: 1,
parallelUploads: 1,
url: "upload.php",
success: function (file,response) {
file.previewElement.classList.add("dz-success");
},
error: function (file,response) {
sv.previewElement.classList.add("dz-error");
}
});
});
正如 dropzone 文檔所說:
由於事件偵聽只能在 Dropzone 實例上完成,因此設置事件偵聽器的最佳位置是在 init 函數中
因此,設置 init 回調並在其中的 dropzone 實例中聲明success
和error
回調:
$("#my-dropzone").dropzone({
maxFiles: 1,
maxFilesize: 1,
parallelUploads: 1,
url: "upload.php",
init: function() { // first you need an init callback
success: function(file, response) {
alert("Yeehaw!");
},
error: function(file, response) { // and then can you have your error callback
alert("Bah humbug...");
}
}
});
考慮這個等效的例子:
try {
var foo = function() {
throw new Error("foo error");
}
}
catch (error) {
}
foo();
您不會期望僅僅因為在定義 foo 時執行是在 try 塊內而捕獲由調用 foo 引起的錯誤。
一種解決方案當然是在可能拋出的函數體內使用 try/catch,如您所示。 另一種解決方案是創建一個可重用的“捕獲包裝器”函數,可能是這樣的:
function catchIfException(func, handler) {
return function() {
try {
var args = Array.prototype.slice.call(arguments);
func.apply(this, args);
}
catch (error) {
handler(error);
}
};
}
$(document).on('change', 'select',
catchIfException(
function(event) {
event.preventDefault();
event.stopPropagation();
throw new Error('callPage method failed');
},
function(error) {
console.log("error: " + error);
}
);
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.