您面临的问题与fabric.js(也不是canvas,甚至不是javascriptbtw)都没有直接关系,而是由于某些浏览器(包括Chrome)确实限制了带有下载的
src锚元素(
<a>)的属性的最大长度属性。
当达到此限制时,您唯一需要的就是控制台中无法捕获的“网络错误”;下载失败,但是作为开发人员的您却不知道。
正如在此 _(您拒绝标记为)_重复项中所建议的,解决方案是直接在可用时直接获取Blob(对于画布,您可以调用其
toBlob()方法,或者首先将dataURI转换为Blob,然后然后从该Blob创建一个对象URL。
Fabricjs似乎
toBlob尚未实现功能,因此在您的确切情况下,您将需要稍后执行。 您可以找到许多脚本将dataURI转换为Blob,MDN的polyfillto
Canvas.toBlob()方法中提供了一个脚本。
然后看起来像这样:
// edited from https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfillfunction 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);


