根据我的测试,标题中的错误只在Google Chrome中出现.我正在对一个大的XML文件进行Base64编码,以便可以下载:

this.loader.src = "data:application/x-forcedownload;base64,"+
                  btoa("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                  +"<"+this.gamesave.tagName+">"
                  +this.xml.firstChild.innerHTML
                  +"</"+this.gamesave.tagName+">");

this.loader是隐藏的.

这个错误实际上是一个很大的变化,因为通常情况下,谷歌浏览器会在btoa次呼叫时崩溃.Mozilla Firefox在这里没有问题,所以这个问题与浏览器有关.

Q:

我曾try 使用Downloadify启动下载,但它不起作用.它不可靠,不会抛出错误以允许调试

推荐答案

如果您有UTF8,请使用它(实际上与SVG源代码一起使用),例如:

btoa(unescape(encodeURIComponent(str)))

例子:

 var imgsrc = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(markup)));
 var img = new Image(1, 1); // width, height values are optional params 
 img.src = imgsrc;

如果需要解码base64,请使用以下命令:

var str2 = decodeURIComponent(escape(window.atob(b64)));
console.log(str2);

例子:

var str = "äöüÄÖÜçéèñ";
var b64 = window.btoa(unescape(encodeURIComponent(str)))
console.log(b64);

var str2 = decodeURIComponent(escape(window.atob(b64)));
console.log(str2);

Note:如果您需要让它在mobile-safari中工作,您可能需要go 掉base64数据中的所有空格……

function b64_to_utf8( str ) {
    str = str.replace(/\s/g, '');    
    return decodeURIComponent(escape(window.atob( str )));
}

2017 Update

This problem has been bugging me again.
The simple truth is, atob doesn't really handle UTF8-strings - it's ASCII only.
Also, I wouldn't use bloatware like js-base64.
But webtoolkit does have a small, nice and very maintainable implementation:

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info
*
**/
var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

    // public method for encoding
    , encode: function (input)
    {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length)
        {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2))
            {
                enc3 = enc4 = 64;
            }
            else if (isNaN(chr3))
            {
                enc4 = 64;
            }

            output = output +
                this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
                this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        } // Whend 

        return output;
    } // End Function encode 


    // public method for decoding
    ,decode: function (input)
    {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length)
        {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64)
            {
                output = output + String.fromCharCode(chr2);
            }

            if (enc4 != 64)
            {
                output = output + String.fromCharCode(chr3);
            }

        } // Whend 

        output = Base64._utf8_decode(output);

        return output;
    } // End Function decode 


    // private method for UTF-8 encoding
    ,_utf8_encode: function (string)
    {
        var utftext = "";
        string = string.replace(/\r\n/g, "\n");

        for (var n = 0; n < string.length; n++)
        {
            var c = string.charCodeAt(n);

            if (c < 128)
            {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048))
            {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else
            {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        } // Next n 

        return utftext;
    } // End Function _utf8_encode 

    // private method for UTF-8 decoding
    ,_utf8_decode: function (utftext)
    {
        var string = "";
        var i = 0;
        var c, c1, c2, c3;
        c = c1 = c2 = 0;

        while (i < utftext.length)
        {
            c = utftext.charCodeAt(i);

            if (c < 128)
            {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224))
            {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else
            {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        } // Whend 

        return string;
    } // End Function _utf8_decode 

}

https://www.fileformat.info/info/unicode/utf8.htm

  • 对于任何等于或低于127(十六进制0x7F)的字符,UTF-8

  • 对于等于或低于2047年的字符(祸不单行0x07FF),使用UTF-8 表示法分布在两个字节上.第一个字节将具有 两个高位置位,第三位清零(即0xC2至0xDF).这个 第二字节将设置顶部比特并且清除第二比特(即 0x80至0xBF).

  • 对于大于等于2048但小于65535的所有字符

Javascript相关问答推荐

获取POS餐厅会话用户/收银员

如何用变量productId替换 1"

我可以后增量超过1(最好是内联)吗?

为什么使用MAX_SAFE_INTEGER生成随机整数时数字分布不准确?

Express.js:使用Passport.js实现基于角色的身份验证时出现太多重定向问题

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

JS生成具有给定数字和幻灯片计数的数组子集

RxJS setTimeout操作符等效

从PWA中的内部存储读取文件

类型脚本中只有字符串或数字键而不是符号键的对象

useNavigation更改URL,但不呈现或显示组件

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

Chart.js-显示值应该在其中的引用区域

不能将空字符串传递给cy.containes()

WhatsApp Cloud API上载问题:由于MIME类型不正确而导致接收&Quot;INVALID_REQUEST";错误

将内容大小设置为剩余可用空间,但如果需要,可在div上显示滚动条

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

变量在导入到Vite中的另一个js文件时成为常量.

对具有相似属性的对象数组进行分组,并使用串连的值获得结果

使用jQuery find()获取元素的属性