将表格textarea下载为XML文件。适用于IE10但不适用于Chrome35或Firefox30

时间:2014-06-26 17:33:29

标签: javascript html xml cross-browser

尝试创建一个按钮(或链接)以从html表单textarea中下载数据并保存为XML文件。我可以解释原因,但它会永远消失。

这是我的工作(仅限IE10)文本示例。我也喜欢在Chrome和Firefox中工作,但到目前为止没有运气,有什么想法吗?

我不需要任何花哨的东西,只需将数据放入变量中,将变量放入新文件中,然后提示SaveAs filename.xml

<html>

<head>
<script lang="javascript">
function download(){
     var xmlContent = document.getElementById("Kitchen").innerHTML; //here we load our xml data 
     var blob = new Blob([xmlContent], {
         type: "text/xml;charset=utf-8;"
     });
    navigator.msSaveBlob(blob, "SaveAsYourFileNameHere.xml");
}
</script>
</head>

<body style="background-color: rgb(225,225,225)">
<br>
<form id="House">
  <textarea id="Kitchen"cols="20" rows="10">This is the Kitchen</textarea>
</form>
<br>
<a href="#" onClick="download()">Download Xml</a><br>
<br>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

Blob仅适用于IE,FF和Chrome查看数据URI

https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs

类似的东西:

<script type="text/javascript">
        function base64_encode(data) {
            //  discuss at: http://phpjs.org/functions/base64_encode/
            // original by: Tyler Akins (http://rumkin.com)
            // improved by: Bayron Guevara
            // improved by: Thunder.m
            // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
            // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
            // improved by: Rafa? Kukawski (http://kukawski.pl)
            // bugfixed by: Pellentesque Malesuada
            //   example 1: base64_encode('Kevin van Zonneveld');
            //   returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
            //   example 2: base64_encode('a');
            //   returns 2: 'YQ=='

            var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
            var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
                    ac = 0,
                    enc = '',
                    tmp_arr = [];

            if (!data) {
                return data;
            }

            do { // pack three octets into four hexets
                o1 = data.charCodeAt(i++);
                o2 = data.charCodeAt(i++);
                o3 = data.charCodeAt(i++);

                bits = o1 << 16 | o2 << 8 | o3;

                h1 = bits >> 18 & 0x3f;
                h2 = bits >> 12 & 0x3f;
                h3 = bits >> 6 & 0x3f;
                h4 = bits & 0x3f;

                // use hexets to index into b64, and append result to encoded string
                tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
            } while (i < data.length);

            enc = tmp_arr.join('');

            var r = data.length % 3;

            return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
        }

        var str="Hi my name is Adam and I love Javascript";
        var str64=base64_encode(str);
        var a = document.createElement('a');
        if(window.navigator.msSaveOrOpenBlob){
            var fileData = str;
            blobObject = new Blob([str]);
            a.onclick=function(){
                window.navigator.msSaveOrOpenBlob(blobObject, 'MyFile.csv');
            }
        }else{
            a.setAttribute('href','data:text/csv;headers=Content-Disposition%3A%20attachment%3B%20filename%3D%22with%20spaces.csv%22%0D%0AContent-Language%3A%20en,%E2%88%9A;base64,' + str64,'MyFile.csv');
        }
        a.appendChild(document.createTextNode('Click to Download'));
        document.body.appendChild(a);
    </script>