在IE8中使用“另存为”execCommand以ANSI格式而不是UCS2-Little Endian保存XML文件

Using "Save As" execCommand in IE8 to save XML file in ANSI format instead of UCS2-Little Endian

本文关键字:另存为 UCS2-Little Endian 文件 XML 保存 格式 IE8 execCommand ANSI      更新时间:2023-09-26

我目前正在尝试仅在Internet Explorer 8中使用Javascript中的"SaveAs"execCommand保存XML数据。我正在使用document.write(xml)在打开窗口后写入xml内容。IE8 中的文档对象使用 saveAs Exec 命令允许用户将 xml 文件保存在本地文件夹中。XML文件已生成,但它没有将编码设置为我需要发送此XML(即ANSI/UTF-8编码)的不同接口的首选类型。我只能将XML保存为UCS2小字节序编码格式。下面是我正在使用的代码。

 var location = "D:''export_to_MCT"+dateTime+".xml";
    var xml = tmpSelectedConfigurationXML.transformNode(xslDoc);// XML Is generated here
   alert(xml);
     **var testlink = window.open  ("about:blank","_blank","toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000, top=10000, width=10, height=10, visible=none");
     testlink.document.write(xml); //fileData has contents for the file
testlink.document.close();
testlink.document.execCommand('SaveAs', false, location);**
testlink.close();

我的目的是将 xml 保存为 ANSI/UTF-8 编码,而不采用 BOM 格式。我不能使用 ActiveX 对象的 Scripting.FileSystemObject 方法。请建议

从 XML 中删除不受支持的字符

由于 XML 文档中不接受所有 UTF-8 字符,因此您需要从生成的任何 XML 中删除任何此类字符。读取 xml 文件后,将 '/[^''x{0009}''x{000a}''x{000d}''x{0020}-''x{D7FF}''x{E000}-''x{FFFD}]+/u' 替换为 ''

或尝试使用testlink.document.charset="UTF-8";