为BASE64 URI支持更好的测试(我可以创造一个JS的base64大恩codeD映像?) [英] A better test for base64 URI support (can I create a large base64-encoded image in JS?)

查看:120
本文介绍了为BASE64 URI支持更好的测试(我可以创造一个JS的base64大恩codeD映像?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Modernizr的检测我们的用户正在运行的浏览器支持的功能,到目前为止,一切顺利。但我碰到一个理论问题进行的base64兼容性测试的时候。这种支持的补丁详细<一个href=\"https://github.com/Modernizr/Modernizr/commit/eb5ca7975792dbbc8717626150f808bf382e19fa\">here,并且除与IE8- 一个奇怪的情况下,工程─只允许连接的base64高达32KB

I'm using Modernizr to detect the features supported in the browser our users are running, so far so good. But I've come up against a theoretical problem when testing for base64 compatibility. The patch for this support is detailed here, and works- except for a weird case with IE8- it only allows base64 encoded images of up to 32KB.

我真的不希望嵌入我的JS文件里面32KB长的base64字符串,它会增加臃肿的一个疯狂的金额。所以,我可以创建一个使用JS一个32KB-无效 - 形象呢?我想重复某种模式的字符串中,直到到达长度32KB,诸如此类的事情。或者,也许采取现有的微小的字符串(如一个在Modernizr的补丁),在此仍然导致有效的图像末尾添加垃圾数据。

I don't really want to embed a 32KB long base64 string inside my JS file, it'll add a crazy amount of bloat. So, could I create a 32KB- valid- image using JS? I'm thinking repeating some kind of pattern within a string until it reaches 32KB in length, that sort of thing. Or maybe taking an existing tiny string (like the one in the Modernizr patch) and adding junk data at the end that still results in a valid image.

我知道旁边的一无所知base64编码,不是如何处理现有的图像等。没有人有任何想法?

I know next to nothing about base64 encoding, other than how to manipulate an existing image. Does anyone have any ideas?

推荐答案

我的认为的我有一个答案。我试过各种方法(在PNG源,我可以手动添加等重复的文本块),直到我发现,添加换行符似乎做的工作:

I think I have an answer. I tried all sorts of techniques (repeated text chunks in the PNG source that I could manually add, etc) until I found that adding line breaks appears to do the job:

    var b64test = new Image();
    b64test.onload = function() {
       alert("yay!")
    }

    b64test.onerror = function() {
       alert("boo")
    }

    /* A 1x1 GIF image */

    var base64str = "R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="
    while (base64str.length < 33000) {
        base64str = "\r\n" + base64str;
    }

    b64test.src= "data:image/gif;base64," + base64str;

在IE8中失败,在IE9等人的作品。我很乐意听到任何的替代品,虽然。

Fails in IE8, works in IE9 and others. I'd love to hear any alternatives, though.

这篇关于为BASE64 URI支持更好的测试(我可以创造一个JS的base64大恩codeD映像?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆