When using document.location.href to save data on the client side, how can I suggest the file name in the browser?

I am trying to export an HTML5 canvas as a PNG file so that it is stored on the user computer.

The following snippet saves the image correctly.

document.location.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); 

However, using Chrome on Mac OS X, it is always saved as a โ€œdownloadโ€. In Safari, this is called "Unknown." Firefox gives the name gobbledygook.

Can I suggest a browser to use a name like "exportedImage.png"?

+6
source share
1 answer

This seems to be a topic of discussion on browser error lists and the W3C mailing list . I cannot find any evidence that any browser supports the idea of โ€‹โ€‹a file name parameter in the "data:" URL, although it is clear that the specification supports the idea of โ€‹โ€‹parameters in general.

Also see this old SO question, which essentially matches up with this.

+4
source

Source: https://habr.com/ru/post/888706/


All Articles