将 UTF8 文本写入文件 [英] Writing UTF8 text to file

查看:37
本文介绍了将 UTF8 文本写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following function to save text to a file (on 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();
}

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

The code works fine if the text is fully Latin-9 but when the text contains even a single UTF-8 encoded character, the write fails.

ActiveX FileSystemObject 似乎不支持 UTF-8.我首先尝试对文本进行 UTF-16 编码,但结果是乱码.什么是解决方法?

The ActiveX FileSystemObject does not support UTF-8, it seems. I tried UTF-16 encoding the text first but the result was garbled. What is a workaround?

推荐答案

试试这个:

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();
}

这篇关于将 UTF8 文本写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆