Search code examples
unicodeasp-classicansi

ASP ANSI Conversion for Unicode Based Text File


I have these ASP script for cache

fn = "caches/"&md5(url)&".html"

// Grab HTML file from site
set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXMLHTTP.Open "GET", url, false
oXMLHTTP.Send

// Save it
set fs = Server.CreateObject("Scripting.FileSystemObject")
set file = fs.CreateTextfile(fn,false,true)
file.Write oXMLHTTP.responseText
file.close

// Open it and print to screen
set file = fs.OpenTextFile(fn,1)
response.write file.ReadAll
file.Close
response.end

Saved files are "Unicode BOM" encoded, and this causes char problems. When I am convert encoding to "ANSI", everything becomes look normal as expected.

How to covert "oXMLHTTP.responseText" to "ANSI" programaticly?


Solution

  • Best practice is saving binary instead of text always. You can access response binary by using oXMLHTTP.responseBody instead.
    However, if you are sure that the response text is compatible with your locale, it is safe to save the response text in ANSI with no problem.
    To do this, you need to pass False for third parameter of CreateTextFile method.

    set file = fs.CreateTextfile(fn, false, false)
    

    But I strongly recommend you to save the binary like the following:

    Dim Stm
    Set Stm = Server.CreateObject("Adodb.Stream")
        Stm.Type = 1 'adTypeBinary, to set binary stream
        Stm.Open
        Stm.Write oXMLHTTP.responseBody ' bytes of http response
        Stm.SaveToFile fn 'response saved as is
        Stm.Close
    Set Stm = Nothing