javascript - Missing extension when saving image from canvas - Stack Overflow

I use this code to save images in Javascript :window.location.href = grid.toDataURL("imagepng&quo

I use this code to save images in Javascript :

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

The code works, but the file saved doesn't have any extension, I have to rename it manually.

My question is how can I put the extension at the end ?

Thank you.

I use this code to save images in Javascript :

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

The code works, but the file saved doesn't have any extension, I have to rename it manually.

My question is how can I put the extension at the end ?

Thank you.

Share Improve this question asked Dec 7, 2013 at 17:47 jbltxjbltx 1,3152 gold badges19 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

toDataURL produces a data-uri not a file name so extensions doesn't apply in this case.

A data-uri is simply a text encoded version of the binary content which some browsers can read as a file - a data-stream if you will. As data streams doesn't have any filename you can't attach one.

You can work around this by setting an anchor tag like this:

<a id="imageLink" href="data-uri-here" download="myFilename.png">
    Click to download
</a>

In JavaScript you can set these attributes dynamic:

imageLink.href = grid.toDataURL();
imageLink.download = 'myOtherFilename.png';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744653555a4586046.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信