Safari 中的 Base64 图像标签未显示
Posted
技术标签:
【中文标题】Safari 中的 Base64 图像标签未显示【英文标题】:Base64 image tag in safari did not showed up 【发布时间】:2015-02-08 08:58:45 【问题描述】:我做了一个 tsp,它将 Image 解码为 base64 字节数组字符串。
它适用于 Chrome 和 Firefox。但是在 safari 8.0 中,它不起作用。
我的 jsp 如下所示:
String sFileInfo = "";
String name = request.getHeader("file-name");
String ext = name.substring(name.lastIndexOf(".")+1);
InputStream is = request.getInputStream();
byte b[] = IOUtils.toByteArray(is);
String base64DataString = Base64.encodeBase64String(b);
base64DataString = "data:image/" + ext + ";base64," + base64DataString;
if(is != null)
is.close();
下面的结果代码将附加到浏览器。
<p><span style="font-size:48px"><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAFoAqQMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAAFAQIDBAYAB//EADgQAAEEAQMBBQUGBgIDAAAAAAEAAgMRBAUSITETQVFhcQYiMoGRFCNCocHRM1JyseHxFWI0U4L/xAAaAQADAQEBAQAAAAAAAAAAAAABAgMEAAUG/8QAJxEAAgICAgICAgIDAQAAAAAAAAECEQMSBCETMSJBMlEUkSNScQX/2gAMAwEAAhEDEQA/AMVSdtTwEu1fTany1jNqXan0upNQLGAJaT6S0jQLI9q7apKXUuo6yPau2qSl1LqOsj2rqUlLqXUdZFtXbVJS6kKDZEWpNql2pKXUdsRFqaQp9qaWpWhtiEtSFqmLU0tStBUiKkikLV1JXEopFoBLSlEafFC10jWveGNJouIule0u2Z9WQUnCJ5buEbi3xDeFpcfD0jHpzQ7J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A53/AMhEtHw4cnNaADtjFm/xLp5qVgx4blQEGnOoc0T3EKwNOpo3BvyK34giLQCxgA6BzbIQzP8AZ9ssZfiPEcl/DfBWVcu3TNz4eq67Mn9ijYbpqUNZe5gIrjormTiSwODMljwGnqK5SBsB43UPPhW2tENadFGR1HgH5qBzQ9vvNPyRJkLS4tNEJ5x2xj3QCT3Jt6F1bBkLNoobfUhWWNYW0SQfBWOxY3qznyU0WDJNVMLd3w2OqWU19hjF+kVsdgMoiY63uNABXJdAy3kuY0OHrSv+z2DJDlySTRtAayhfPKNyEyNPhfgs+TM4y+Jrx4VKPyPNszDkjlcx4LXN6gqr9md4L0rJwcBzXuMEbpHdXOu7QX/gJfHH+p/ZXhyotdmbJw5X0XPsbmAB0bee9Pj0lpNkok/olj+ELz/JI3+KJFHhMiADTakELBzXIUgTT1SbNlKSIZYXPFWaPmh82AQ643UR3hF+5Upu/wBE8JMnkimgdI9zWkNdyDyUNyYZJnFx59SiE3wD1Km0cAzyA9CBY+q1KWqsySju9WBhgykBxZx9Aiug4xjlMgcAQaII/VaGRrTjuto6eHkslE9+13vO+qXyvJFoPijhkmaeWQGO7N+KrDOkjIDjYUOKb0+EnwVSYneeVBR7o0yyOrDDjDnRgSxh+3kBVMjScbY5zYiw9a7lHpxPadT1RLNJ7IG0LcXSYaU420ZF3YtkdYI8qUjGsmdtZfzTdRN5Tr8Anaf/ABFuf42ecvz1LmHp8na3PEXxgdxR7FALBbPyUeN8ZHdSst+KvJYck3J9npYsaiuh4cwNIPXzVOfJiicGXyeU8/Es3rJP29ws0uxQ2dAzZHCNh7cJB5eKXsx4n6Ktgf8Ajs9FbQl06DF2rP/Z" style="height:90px; width:169px" />1<span style="background-color:#00FFFF">zxczxczc</span></span></p>
我复制这段代码(base64;
之后的一部分)并将链接放在下面的 safari 中。
http://base64online.org/decode/
我得到了一个图像,所以我假设 Safari 也支持 base64 图像。
但是,当我将它放在带有图像标签的网络上时,它不起作用。
感谢您的回答:D
PS浏览器报错信息是
Failed to load resource: (kCFErrorDomainCFNetwork error -10.)
【问题讨论】:
【参考方案1】:我知道这个问题已经很老了,但我最近在 ios 的 safari 上遇到了类似的问题,问题似乎是 Safari 不会渲染那些字符数不能被 4 整除的 base64 图像。
解决此问题的方法是在末尾使用 '=' 字符填充编码字符串。这是一个基本算法:
// b64str = 's/3eea4sp...' (or any base 64 encoded string)
while (b64str.length % 4 > 0)
b64str += '=';
希望这对某人有所帮助!
【讨论】:
这对我不起作用。我现在只是收到损坏的图片链接。【参考方案2】:我遇到了类似的情况,但发布的解决方案对我不起作用。但经过反复试验,我确实想出了一个替代解决方案。希望这会有所帮助。
// Add an actual base64 string
var encodedImgString = 'data:image/png;base64,iVBORw0KGgoAAA...';
// Create an image, set img source and cross origin attribute
var iosImg = new Image;
iosImg.src = encodedImgString;
iosImg.crossOrigin = 'Anonymous';
// Change this to target your element and add it wherever you need it to appear
document.body.appendChild(iosImg);
【讨论】:
【参考方案3】:@SylvainB - 我认为它需要被 3 或 4 整除才能在 Safari 中呈现 @IHan - 确保在进行可分性测试时排除 'data:image/png;base64,' 部分
【讨论】:
以上是关于Safari 中的 Base64 图像标签未显示的主要内容,如果未能解决你的问题,请参考以下文章
在一个 img src 标签上显示不同格式的普通图像,以及 base64 图像
如何转换数据:image / webp,expo(或php)中的base64