javascript 强制下载 base64 镜像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27327965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
force download base64 image
提问by user2334436
To start with JS isn't one of my strengths. I've been trying for the past couple of days to edit this JS function to make it force download base64 image. What the function does, when the download button is clicked, is open a new window with the image on it. The user then has to right click and save the picture. I'm trying to force download the image instead of right click and ‘save as'.
从 JS 开始并不是我的强项之一。过去几天我一直在尝试编辑此 JS 函数以使其强制下载 base64 图像。当单击下载按钮时,该函数的作用是打开一个带有图像的新窗口。然后用户必须右键单击并保存图片。我试图强制下载图像而不是右键单击并“另存为”。
The dataurl produce base4 png string (data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQ………)
dataurl 生成 base4 png 字符串(data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQ……)
i tried using as it was suggested in another thread but that didn't work.
我尝试按照另一个线程中的建议使用,但是没有用。
All suggestions are welcomed. Thanks.
欢迎所有建议。谢谢。
savePaint: function() {
var self = this;
dataURL = self.context.canvas.toDataURL();
var cntnt = $("<p class='dialogHeader'>Please right click and select 'Save Image As' option. Click here to Return</p> <img id='PrintImage' src=" + dataURL + ">");
self.newSavedImage.html(cntnt);
self.showPopup(self.newSavedImage, self.canvasWidth, self.canvasHeight)
}
回答by user2334436
Finally! I got it to work. For anyone facing the same problem I found a function here http://danml.com/download.htmlthat will let you force download the base64.
最后!我让它工作。对于任何面临同样问题的人,我在这里找到了一个功能http://danml.com/download.html可以让您强制下载 base64。
回答by JasonCG
You could specify a different MIME type rather than image/jpeg so the browser doesn't attempt to open the image natively:
您可以指定不同的 MIME 类型而不是 image/jpeg,这样浏览器就不会尝试在本机打开图像:
data:application/octet-stream;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/...
This will force the file to download, but I noticed in some browser configurations may autosave with a default filename and since the browser doesn't know the real file type it will be up to the user to specify the correct type. Unfortunately the data URI scheme does not allow for a suggested file name.
这将强制下载文件,但我注意到在某些浏览器配置中可能会使用默认文件名自动保存,并且由于浏览器不知道实际文件类型,因此由用户指定正确的类型。不幸的是,数据 URI 方案不允许使用建议的文件名。
Check out Using HTML5/Javascript to generate and save a filefor more possible solutions. Your best bet, depending on your requirements, might be to go with a Flash-based solution such as Downloadify.
查看使用 HTML5/Javascript 生成并保存文件以获得更多可能的解决方案。根据您的要求,您最好的选择可能是使用基于 Flash 的解决方案,例如Downloadify。