About the invalid download of a tag (canvas)

has obtained a canvas, and then downloaded and saved it, but the result part cannot be downloaded.
the size of the picture is 4m, while it is said online that the href length of an is limited.
how to download

function aa(src,imgname){
    var alink=document.createElement("a");
    alink.href=src;
    alink.download=imgname;
    alink.click();
    alink.remove();
}
var url = canvas.toDataURL("image/png");
aa(url,"");
Dec.08,2021

it is recommended to use the toBlob method, which can convert canvas into Blob files, which is usually used in file uploads because it is binary and more back-end friendly.

Note that toBlob () is asynchronous, so there will be a callback

canvas.toBlob(function(blob) {
  var url = URL.createObjectURL(blob);
  a.href = data;
  a.download = 'bg.png';
})
Menu