使用html2canvas 生成尺寸較大 base64 后進行 a標簽 download 下載 ,瀏覽器報網絡失敗錯誤
通過谷歌搜索 發現原因是
因為截取尺寸較大 導致生成base64 長度太大 ,達到了a標簽的href 上限,所以報錯下載失敗,解決方案是 將base64 dataURI轉換為Blob 文件對象
然后a 鏈接下載 blob文件路徑
// edited from https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfill function dataURIToBlob(dataURI, callback) { var binStr = atob(dataURI.split(',')[1]), len = binStr.length, arr = new Uint8Array(len); for (var i = 0; i < len; i++) { arr[i] = binStr.charCodeAt(i); } callback(new Blob([arr])); } var callback = function(blob) { var a = document.createElement('a'); a.download = fileName; a.innerHTML = 'download'; // the string representation of the object URL will be small enough to workaround the browser's limitations a.href = URL.createObjectURL(blob); // you must revoke the object URL, // but since we can't know when the download occured, we have to attach it on the click handler.. a.onclick = function() { // ..and to wait a frame requestAnimationFrame(function() { URL.revokeObjectURL(a.href); }); a.removeAttribute('href') }; }; dataURIToBlob(yourDataURL, callback);
解決鏈接:https://stackoverflow.com/questions/37135417/download-canvas-as-png-in-fabric-js-giving-network-error