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

javascript - How to control base64 images when user saves as - Stack Overflow

programmeradmin3浏览0评论

I'm making an app where i want to show the user a range of base64 encoded picture. They are shown by using data-url. If the user wants to save a picture, it can simply rightclick and save as (or drag it into a folder).

Are there any way to control the sugested filename? Right now Chrome just sugest "download" and doesn't even recognize it as an image. Can this be done in any way?

I only need it to work in Chrome.

Thanks for any help

I'm making an app where i want to show the user a range of base64 encoded picture. They are shown by using data-url. If the user wants to save a picture, it can simply rightclick and save as (or drag it into a folder).

Are there any way to control the sugested filename? Right now Chrome just sugest "download" and doesn't even recognize it as an image. Can this be done in any way?

I only need it to work in Chrome.

Thanks for any help

Share Improve this question asked Mar 3, 2012 at 20:58 JPugeJPuge 5961 gold badge5 silver badges14 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Make sure you are setting the proper mime type for the image in the data uri. Doing this fixed the file extension in Chrome 17, for me. The file was downloaded as "download.png", and opened fine.

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="/>

However, if you really need full control over the filename, you can exploit the html5 "download" attribute of the a tag, to set the filename, and create a Blob object with your image data that will be used be downloaded.

See a demo here: http://updates.html5rocks./2011/08/Downloading-resources-in-HTML5-a-download

Read more here: http://dev.w3/2009/dap/file-system/file-writer.html

The current specification does not make this possible.

发布评论

评论列表(0)

  1. 暂无评论