1. 解决toBlob(), 放在你的代码toBlob即可
if (!HTMLCanvasElement.prototype.toBlob) { Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', { value: function value(callback, type, quality) { const canvas = this; setTimeout(() => { const binStr = atob(canvas.toDataURL(type, quality).split(',')[1]); const len = binStr.length; const arr = new Uint8Array(len); for (let i = 0; i < len; i++) { arr[i] = binStr.charCodeAt(i); } callback(new Blob([arr], { type: type || 'image/png' })); }); }, }); }
2. 下载:
if (navigator.msSaveBlob) { // deal with IE 11, data是第一步toBlob的结果值 window.navigator.msSaveOrOpenBlob(data, fileName); } else { const a = document.createElement('a'); document.body.appendChild(a); a.download = fileName; a.href = window.URL.createObjectURL(data); a.click(); a.remove(); }