I'm using javascript and html canvas to resize jpeg images. After resizing, I use canvas.toDataURL
as the href attribute in an anchor tag in order to provide a link where users can download the resized images.
This works nicely up to a certain image size.
It seems that different browsers have different limits on the size of data urls as mentioned here: https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
In chrome, when I'm over the data url size limit, nothing happens when I click on the download link; no errors or anything (as far as I can tell).
Is there a some way to programmatically detect whether a data url is too large? Maybe some browser event that will show me whether clicking a download link failed?
Ideally, I'd like to detect whether the download was successful. When data urls are too large, I'd like to display an explanation to the end user describing how to right click on the image and choose "save as ...", which always seems to work.
Update 1:
It looks like using canvas.toBlob
is the best workaround for now. Here's the api documentation: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob.
Here's a jsfiddle that demonstrates how anchor href download links fail when using toDataURL
for larger canvases, but toBlob
seems to work:
https://jsfiddle.net/upgradingdave/c76q34ac/3/
Here are some related stackoverflow questions: canvas.toDataURL() download size limit