Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using document.location.href to save client-side data, how can I suggest a filename to the browser?

I'm trying to export an HTML5 canvas as a PNG file, so that it is stored on the user's 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 always is saved as "download". In Safari it is called "Unknown". Firefox gives it a gobbledygook name.

Can I suggest to the browser a name to use, such as "exportedImage.png"?

like image 729
Steve McLeod Avatar asked May 21 '11 15:05

Steve McLeod


1 Answers

This is apparently a topic of discussion in browser bug lists and a W3C mailing list. I can't find any evidence that any browser supports the idea of a filename parameter in a "data:" URL, though it is clear that the spec supports the idea of parameters in general.

Also see this older SO question, which is essentially the same as this one.

like image 80
Pointy Avatar answered Oct 21 '22 12:10

Pointy