首页 > 代码库 > base64编码用在HTML(支持IE6、IE7、IE8)

base64编码用在HTML(支持IE6、IE7、IE8)

  1. 在HTML使用base64编码, 叫做Data URI scheme
  2. Data URI scheme目前支持的类型有:(2016-10-19)
    data:,文本字串
    data:text/plain,%s
    data:text/html,%s
    data:text/html;base64,%s
    data:text/css,%s
    data:text/css;base64,%s
    data:text/javascript,%s
    data:text/javascript;base64,%s
    data:image/gif;base64,%s
    data:image/png;base64,%s
    data:image/jpeg;base64,%s
    data:image/x-icon;base64,%s


  3. 1)在 linux 下直接可以使用 base64 命令行工具产生 base64 编码:
    > base64 "/user/prj/64 tester/test.png"
    2)在 PHP 中可以用函数 base64_encode() 进行编码:
    <?php
        $s = ‘123‘;
        echo base64_encode($s);
        // 附:反编译 : base64_decode($s);
    ?>
    3)在 HTML5 下的浏览器运用 FileReader 进行编码:
    <input type="file" onchange="readfile4base64uri(this)" />
    <script type="text/javascript">
    function readfile4base64uri(el) {
        var f = el.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(f);
        reader.onload = function(e) {
            var s = this.result;
            alert(s);
            // var _tmp = ‘<img src="http://www.mamicode.com/‘ + s + ‘" class="ya-Ma-dieee" />‘;
            // $(‘preview-panel‘).innerHTML = _tmp;
        };
    }
    </script>
    4) 用 csharp 的话, 可以这样:
    static public string base64(string s, bool b)
    {
        /***
        * bool b, When [true] 加密, otherWise [false] 解密
        * author: xhx jx744418777
        * OnDate: 20161019 [C# vs2010 WinXp sp3]
        ***/
        try
        {
            if (b) {
                    return System.Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(s));
            } else {
                    return System.Text.Encoding.Default.GetString(System.Convert.FromBase64String(s));
            }
        } catch(Exception exp)
        {
            return exp.Message;
         }
        return null;
    }
    5)C\C++, 参考 CSDN 上的:
            http://blog.csdn.net/tiandyoin/article/details/30723475
            《[转] Base64加密解密算法的C/C++代码实现》

  4. base64 浏览器支持程度:
      Opera 7.2+ data URI 必须小于4100字符
      IE8+ data URI必须小于32k(IE8不支持js的data URI(用js的话要根据具体浏览器和系统编写,

    我在WinXp IE6下写时有些bug, 若只是使用 javascript 地址加入: file:// 前缀才行)

      ChromeFFSafari无限制

  5. 1)
    http://blog.csdn.net/hansel/article/details/32223269
    《[原] Chrome保存网页为mhtml格式》
    I. 打开chrome,在地址栏输入chrome://flags,
    II. 找到“Save Page as MHTML”,点击[enable],重启chrome,搞定。
    III.打开这个选项后,保存网页只能保存为mhtml格式。

    2)在 IE6 、 IE7 下参考:
    Content-Type: multipart/related;
    type="text/html";
    boundary="----=_Part_A47D6C30"
    X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5994

    ------=_Part_A47D6C30
    Content-Type: text/html; charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Location: =?utf-8?Q?index.html?=

    =EF=BB=BF<!DOCTYPE HTML>
    <html lang=3D"en-US">
    <head>
    <meta charset=3D"UTF-8">
    <title>Document</title>
    <style type=3D"text/css">
    .uptip {
    width: 150px;
    background: #cdf url(test.png) no-repeat right center;
    }
    </style>

    <script type=3D"text/javascript">
    alert(888);
    window.onerror =3D function(line){
    alert(line);
    };
    </script>
    <script type=3D"text/javascript" src=http://www.mamicode.com/3D"test.js"></script>
    </head>
    <body>
    <p>1. TEXT tag:&nbsp;&nbsp;<span>2131231</span></p>
    <p>2. IMG tag:&nbsp;&nbsp;<IMG alt=3D"img tag" src=http://www.mamicode.com/3D"test.png"></p>
    <p class="3D""uptip">3. Css background:&nbsp;&nbsp;<span ></span></p>
    <p>3. test javascript:&nbsp;&nbsp;<span id="testjs"></span></p>
    </body>
    </html>

    ------=_Part_A47D6C30
    Content-Type: image/png
    Content-Transfer-Encoding: base64
    Content-Location: =?utf-8?Q?test.png?=

    iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
    bWFnZVJlYWR5ccllPAAAAdFJREFUeNqkk71LHEEYxt/Zr1u4O46gIlhITJMmReCwOFGjhQTSaEgj
    CGmyhYWlTYIg9lYhEA3nf6CohYJooSRaiKISFY1fUUEil+hy7t3t7XzlHTGghe4dGXjmhd35PTvv
    MztESgn/Mww1nX3r78DyDvWiRG4BNVLTODBBThf6Og07kX70pMmOxKt0CNsRIVC8ynD3aMmnhQvH
    YIw5lXUp27K4Lgr7AFKEGGhgWVE98bje/vV9yjE450nLRLh4DiCKuCIsEwJSi4BlVuuKNRilumAe
    SJovAb4ZnGMnHijWoIyB4BQkZ2WlT5BRrEGpgjmalGegIaNYZcA5QxNWnoHUqDLgBg2C1VzWbbFN
    ppecAQYZeFmB7JaGLunDrTU/5/lcMGxFZfKgOBTygTjcXveRHSHqV54detOFtg4qed83kw2pGCGS
    /M649OePnXV8lG7rHvtCwu7C9Kf256Zlf61vao1J5sPmxmb+ys00v+qZXL0OM6xbPGun7llLRB3S
    3u6ef5k5G/4HX6fx0A7GB1++rX2aGo7Go/rxznKQ97IfX/fOfLgT520DQoiNJYaKVyQilZ/fN88L
    CebByeXc4sb56PTS6Qq++4Nykcsp5q8AAwDKgQwKmGYUpAAAAABJRU5ErkJggg==

    ------=_Part_A47D6C30--

  6. 【参考】
    1、 http://www.jb51.net/article/50789.htm <神盾加密解密教程(三)PHP 神盾解密工具>
    2、 http://music.163.com/#/playlist?id=387178008&userid=5776820 <网易音乐 3D你的耳朵>
    3、 https://github.com/mangui/flashls/releases/ <hls(m3u8) flash v0.4.4.21>
    4、 http://blog.csdn.net/hansel/article/details/32223269 <Chrome保存网页为mhtml格式>

  7. The End.

    写zuo历时: <13:15:00 ~ 15:31:00> ON [20161019] Ver [v0.01]

base64编码用在HTML(支持IE6、IE7、IE8)