ie下不支持File、canvas的toBlob

ie下使用canvas截图,执行toBlob报错

// toBlob兼容ie
if (!HTMLCanvasElement.prototype.toBlob) {
    Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
        value: function (callback, type, quality) {
            var canvas = this;
            setTimeout(function() {
                var binStr = atob( canvas.toDataURL(type, quality).split(',')[1] );
                var len = binStr.length;
                var arr = new Uint8Array(len);

                for (var i = 0; i < len; i++) {
                    arr[i] = binStr.charCodeAt(i);
                }

                callback(new Blob([arr], { type: type || 'image/png' }));
            });
        }
    });
}

ie下执行new File()报错,使用typeof File检测,ie下为object,chrome下为function

  • 使用继承实现File
// File兼容ie
// File兼容ie
if (typeof File !== "function") {
    File = function (chunks, filename, opts) {
        var _this;

        if (opts === void 0) {
            opts = {};
        }

        _this = Blob.call(this, chunks, opts) || this;
        _this.lastModifiedDate = new Date();
        _this.lastModified = +_this.lastModifiedDate;
        _this.name = filename;
        return _this;
    };
    File.prototype = (function () {
        function fn() {
        }

        fn.prototype = Blob.prototype;
        return new fn;
    }());
    File.prototype.constructor = Blob;
}
posted @ 2021-03-17 11:39  Lanomw  阅读(540)  评论(0编辑  收藏  举报