将UTF8文本写入文件

son*_*phi 4 javascript unicode internet-explorer activex internationalization

我使用以下函数将文本保存到文件(在IE-8 w/ActiveX上).

function saveFile(strFullPath, strContent)
{
    var fso = new ActiveXObject( "Scripting.FileSystemObject" );

    var flOutput = fso.CreateTextFile( strFullPath, true ); //true for overwrite
    flOutput.Write( strContent );
    flOutput.Close();
}
Run Code Online (Sandbox Code Playgroud)

如果文本完全是Latin-9,但是当文本甚至包含单个UTF-8编码字符时,代码工作正常,则写入失败.

似乎ActiveX FileSystemObject不支持UTF-8.我首先尝试使用UTF-16编码文本,但结果是乱码.什么是变通方法?

Mat*_*ens 5

试试这个:

function saveFile(strFullPath, strContent) {
 var fso = new ActiveXObject("Scripting.FileSystemObject");
 var utf8Enc = new ActiveXObject("Utf8Lib.Utf8Enc");
 var flOutput = fso.CreateTextFile(strFullPath, true); //true for overwrite
 flOutput.BinaryWrite(utf8Enc.UnicodeToUtf8(strContent));
 flOutput.Close();
}
Run Code Online (Sandbox Code Playgroud)

  • Utf8Lib凭空而来?没有安装在我们公司. (2认同)