a標籤 download base64 下載 網絡失敗

使用html2canvas 生成尺寸較大 base64 後進行 a標籤  download 下載 ,瀏覽器報網絡失敗錯誤html

經過谷歌搜索 發現緣由是canvas

由於截取尺寸較大  致使生成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-errorspa

相關文章
相關標籤/搜索