最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Create a Save as button for an image - Stack Overflow

programmeradmin1浏览0评论

I'm developing a Screenshot extension for Chrome, and would like to know how could I implement a 'Save as' button in Html/Javascript. Currently users only Right Click To 'Save as'. I'm new to Javascript & didn't exactly find a way how to do it online. Any ideas? Thanks!

I'm developing a Screenshot extension for Chrome, and would like to know how could I implement a 'Save as' button in Html/Javascript. Currently users only Right Click To 'Save as'. I'm new to Javascript & didn't exactly find a way how to do it online. Any ideas? Thanks!

Share Improve this question asked Mar 7, 2015 at 14:31 fcs132fcs132 491 silver badge3 bronze badges 1
  • 1 please post what you have tried so far. – Banana Commented Mar 7, 2015 at 14:39
Add a ment  | 

2 Answers 2

Reset to default 5

You can use the HTML5 download attribute, is it what you are looking for ?

<img src="http://blog.grio./wp-content/uploads/2012/09/stackoverflow.png" alt="Stack Overflow">
<br />
<a href="http://blog.grio./wp-content/uploads/2012/09/stackoverflow.png" download>Save !</a>

You can create a download link with <a href="" download="yourFileName">Save as …</a>. The download attribute will cause the resource to be downloaded instead of viewed in the browser.

Then, if you want the link to actually refer to the resource, use something like yourAnchorNode.href = yourImageNode.src;, or if you use a <canvas> element to display the picture (you didn’t specify it in your original question, unfortunately) use toDataURL or toBlob:

yourAnchorNode.href = yourCanvasNode.toDataURL();

or

yourCanvasNode.toBlob((blob) => yourAnchorNode.href = URL.createObjectURL(blob));

Note that toBlob is an asynchronous operation.

发布评论

评论列表(0)

  1. 暂无评论