如何使用 JavaScript 将 Base64 图像保存到用户磁盘?
我已使用 JavaScript 将源内容从
html 标记转换为 base64String。图像显示清晰。现在我想使用 JavaScript 将该图像保存到用户的磁盘上。
<html>
<head>
<script>
function saveImageAs () {
var imgOrURL;
embedImage.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" +
"AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" +
"9TXL0Y4OHwAAAABJRU5ErkJggg==";
imgOrURL = embedImage;
if (typeof imgOrURL == 'object')
imgOrURL = embedImage.src;
window.win = open(imgOrURL);
setTimeout('win.document.execCommand("SaveAs")', 0);
}
</script>
</head>
<body>
<a href="#" ONCLICK="saveImageAs(); return false" >save image</a>
<img id="embedImage" alt="Red dot">
</body>
</html>
当我将图像路径设置为
html 标记的源时,此代码运行良好。但是,当我将源作为 base64String 传递时不起作用。
如何实现我想要的?
I have converted the source content from the <img>
html tag to a base64String using JavaScript. The image was displayed clearly. Now I want to save that image to user's disk using javascript.
<html>
<head>
<script>
function saveImageAs () {
var imgOrURL;
embedImage.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" +
"AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" +
"9TXL0Y4OHwAAAABJRU5ErkJggg==";
imgOrURL = embedImage;
if (typeof imgOrURL == 'object')
imgOrURL = embedImage.src;
window.win = open(imgOrURL);
setTimeout('win.document.execCommand("SaveAs")', 0);
}
</script>
</head>
<body>
<a href="#" ONCLICK="saveImageAs(); return false" >save image</a>
<img id="embedImage" alt="Red dot">
</body>
</html>
This code worked well when I set the image path as source for <img>
html tag. However, when I pass the source as base64String does not work.
How to achieve what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
HTML5
download
属性为了允许用户下载图像或其他文件,您可以使用 HTML5
download
属性。静态文件下载
动态文件下载
在动态请求图像的情况下,可以模拟此类下载。
如果您的图像已加载并且您有
base64
源,则:否则,如果图像文件下载为
Blob
,您可以使用FileReader
来转换它到 Base64:Firefox
您创建的锚标记还需要添加到 Firefox 中的 DOM,以便被点击事件识别(链接)。
不支持 IE:Caniuse 链接
HTML5
download
attributeJust to allow user to download the image or other file you may use the HTML5
download
attribute.Static file download
Dynamic file download
In cases requesting image dynamically it is possible to emulate such download.
If your image is already loaded and you have the
base64
source then:Otherwise if image file is downloaded as
Blob
you can useFileReader
to convert it to Base64:Firefox
The anchor tag you are creating also needs to be added to the DOM in Firefox, in order to be recognized for click events (Link).
IE is not supported: Caniuse link
在 JavaScript 中,您无法直接访问文件系统。
但是,您可以让浏览器弹出一个对话框窗口,允许用户选择保存位置。为此,请使用 Base64String 的
replace
方法,并将"image/png"
替换为"image/octet-stream"
:此外,符合 W3C 的浏览器提供了 2 种处理 Base64 编码和二进制数据的方法:
也许,你会发现它们在某种程度上很有用...
这是我理解您需要的重构版本:
In JavaScript you cannot have the direct access to the filesystem.
However, you can make browser to pop up a dialog window allowing the user to pick the save location. In order to do this, use the
replace
method with your Base64String and replace"image/png"
with"image/octet-stream"
:Also, W3C-compliant browsers provide 2 methods to work with base64-encoded and binary data:
Probably, you will find them useful in a way...
Here is a refactored version of what I understand you need:
这有效
基于上面的答案,但有一些变化
This Works
Based on the answer above but with some changes
查看 https://github.com/eligrey/FileSaver.js/ ,它包装了HTML5 方法并为 IE10 等提供解决方法。
Check out https://github.com/eligrey/FileSaver.js/ which wraps the HTML5 method and provides workarounds for e.g. IE10.