开发者

Writing UTF8 text to file

开发者 https://www.devze.com 2022-12-30 22:51 出处:网络
I am using the following function to save text to a file (on IE-8 w/ActiveX). function saveFile(strFullPath, strContent)

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

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开发者_开发技巧.

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?


Try this:

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


The CreateTextFile method has a third parameter which decides whether file be written unicode or not. You can do like:

var flOutput = fso.CreateTextFile(strFullPath,true, true);

Interestingly, way back I had created this little script to save files in unicode format:

Set FSO=CreateObject("Scripting.FileSystemObject")
Value = InputBox ("Enter the path of the file you want to save in Unicode format.")

If Len(Trim(Value)) > 0 Then
    If FSO.FileExists(Value) Then
        Set iFile = FSO.OpenTextFile (Value)
        Data = iFile.ReadAll
        iFile.Close

        Set oFile = FSO.CreateTextFile (FSO.GetParentFolderName(Value) & "\Unicode" & GetExtention(Value),True,True)
        oFile.Write Data
        oFile.Close

        If FSO.FileExists (FSO.GetParentFolderName(Value) & "\Unicode" & GetExtention(Value)) Then
            MsgBox "File successfully saved to:" & vbCrLf & vbCrLf &  FSO.GetParentFolderName(Value) & "\Unicode" & GetExtention(Value),vbInformation
        Else
            MsgBox "Unknown error was encountered!",vbCritical
        End If
    Else
        MsgBox "Make sure that you have entered the correct file path.",vbExclamation
    End If
End If

Set iFile = Nothing
Set oFile= Nothing
Set FSO= Nothing

Function GetExtention (Path)
    GetExtention = Right(Path,4)
End Function

Note: This is VBScript code, you should save that code in a file like unicode.vbs, and once you double click that file, it will run.


Add a third parameter, true, in your call to the CreateTextFile method. See this page.


function saveFile(strFullPath, strContent) {
    var fso = new ActiveXObject( "Scripting.FileSystemObject" );
    var flOutput = fso.CreateTextFile( strFullPath, true, true ); //true for overwrite // true for unicode
    flOutput.Write( strContent );
    flOutput.Close();
}

object.CreateTextFile(filename[, overwrite[, unicode]])

0

精彩评论

暂无评论...
验证码 换一张
取 消