wzhanjun / blog

my person notes
0 stars 0 forks source link

plupload组件上传 #51

Open wzhanjun opened 3 years ago

wzhanjun commented 3 years ago

plupload 新增过滤器


   // 添加过滤
    Plupload.addFileFilter('width_height', function(data, file, cb) {
        var self = this, img = new moxie.image.Image();
        function finalize(result) {
            // cleanup
            img && img.destroy();
            img = null;

            // if rule has been violated in one way or another, trigger an error
            if (!result) {
                self.trigger('Error', {
                    code : Plupload.IMAGE_DIMENSIONS_ERROR,
                    message : "图片尺寸不一致, 尺寸要求, 宽:" + data.width + "px, 高: " + data.height + " px.",
                    file : file
                });
            }
            cb(result);
        }
        img.onload = function() {
            // check if resolution cap is not exceeded
            console.log("image:", img.width, img.height, "data:", data)
            var result = true;
            if (data.width > 0 && data.height > 0) {
                result = (img.width == data.width) && (img.height == data.height)
            }
            finalize(result);
        };
        img.onerror = function() {
            finalize(false);
        };
        img.load(file.getSource());
    });

参考: https://chaping.github.io/plupload/doc/#plupload_doc2 https://www.coder.work/article/2253194 https://blog.sylingd.com/archives/271.html