如何在dropzone中为maxfilesize抛出错误?

时间:2018-05-24 10:07:06

标签: javascript dropzone

在我的代码中我设置了文件大小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");
            }
        });
    });

2 个答案:

答案 0 :(得分:0)

考虑这个等效的例子:

try {
    var foo = function() {
        throw new Error("foo error");
    }
}
catch (error) {
}

foo();

你不会指望调用foo导致的错误被捕获只是因为foo被定义时执行是在try块内。

一种解决方案当然是在可能抛出的函数体内使用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);
            }
        );
    );

答案 1 :(得分:0)

作为Dropzone文档says

  

由于只能在Dropzone实例上监听事件,   设置事件监听器的最佳位置是init函数

因此,请设置init回调,并在其中的dropzone实例中声明successerror回调:

$("#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...");
        }
    }
});