Opening UTF-8 file result in strange chars

Discussion in 'ASP General' started by Lasse Edsvik, Sep 3, 2007.

  1. Lasse Edsvik

    Lasse Edsvik Guest

    Hello

    I have a slight problem, I'm trying to open a textfile that has been saved
    as UTF-8. But when I run it it displays strange chars eventhough i've
    specified that it should read the file as UTF-8 using the OpenTextFile
    method of the filesystemobject. I need to open the file, then insert data to
    a database, nothing will get printed out on screen so its not a html-charset
    problem.

    How do I fix this?

    TIA
    /Lasse
    Lasse Edsvik, Sep 3, 2007
    #1
    1. Advertising

  2. Lasse Edsvik

    Evertjan. Guest

    Lasse Edsvik wrote on 03 sep 2007 in
    microsoft.public.inetserver.asp.general:

    > I have a slight problem,


    Why slight, Lasse?

    > I'm trying to open a textfile that has been
    > saved as UTF-8. But when I run it it displays strange chars eventhough
    > i've specified that it should read the file as UTF-8 using the
    > OpenTextFile method of the filesystemobject. I need to open the file,
    > then insert data to a database, nothing will get printed out on screen
    > so its not a html-charset problem.
    >
    > How do I fix this?


    What do you mean by "open a textfile" in ASP?

    Show us your code [ONLY the relevant workingpart please]

    What do you mean by "run it" after you open it?
    Can you "run" a text file, or do you mean an ASP or HTML file?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Sep 3, 2007
    #2
    1. Advertising

  3. Lasse Edsvik

    Lasse Edsvik Guest

    Evertjan,

    as a tested with:

    (file is saved as UTF-8 )

    <%
    Set fs=Server.CreateObject("Scripting.FileSystemObject")

    Set f=fs.OpenTextFile(Server.MapPath("/files/temp/test.csv"), 1 , -1)
    Response.Write(f.ReadAll)
    f.Close

    Set f=Nothing
    Set fs=Nothing
    %>


    Resulted in:

    Firstname Surname Email
    Ã¥Ã"ööööÃ- Ã-ööÃ-ä labb01@labb.100procent.com


    the file contains:

    Firstname Surname Email
    åÄööööÖ ÖööÖä labb01@labb.100procent.com







    "Evertjan." <> wrote in message
    news:Xns99A0ABBD13C87eejj99@194.109.133.242...
    > Lasse Edsvik wrote on 03 sep 2007 in
    > microsoft.public.inetserver.asp.general:
    >
    > > I have a slight problem,

    >
    > Why slight, Lasse?
    >
    > > I'm trying to open a textfile that has been
    > > saved as UTF-8. But when I run it it displays strange chars eventhough
    > > i've specified that it should read the file as UTF-8 using the
    > > OpenTextFile method of the filesystemobject. I need to open the file,
    > > then insert data to a database, nothing will get printed out on screen
    > > so its not a html-charset problem.
    > >
    > > How do I fix this?

    >
    > What do you mean by "open a textfile" in ASP?
    >
    > Show us your code [ONLY the relevant workingpart please]
    >
    > What do you mean by "run it" after you open it?
    > Can you "run" a text file, or do you mean an ASP or HTML file?
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)
    Lasse Edsvik, Sep 3, 2007
    #3
  4. Lasse Edsvik wrote:

    > as a tested with:
    >
    > (file is saved as UTF-8 )
    >
    > <%
    > Set fs=Server.CreateObject("Scripting.FileSystemObject")


    FileSystemObject does not support UTF-8, its Unicode support means
    UTF-16 I think.
    You might need to use ADODB.Stream to read in the UTF-8 encoded file.


    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 3, 2007
    #4
  5. Lasse Edsvik

    Evertjan. Guest

    Lasse Edsvik wrote on 03 sep 2007 in
    microsoft.public.inetserver.asp.general:

    > Evertjan,


    >> What do you mean by "open a textfile" in ASP?
    >>
    >> Show us your code [ONLY the relevant workingpart please]


    [please do not toppost on usenet and do not quote signatures]

    > Evertjan,
    >
    > as a tested with:
    >
    > (file is saved as UTF-8 )
    >
    > <%
    > Set fs=Server.CreateObject("Scripting.FileSystemObject")


    Read this:

    http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr06/hey04
    19.mspx


    >> What do you mean by "run it" after you open it?
    >> Can you "run" a text file, or do you mean an ASP or HTML file?


    ??


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Sep 3, 2007
    #5
  6. Lasse Edsvik

    Lasse Edsvik Guest

    That's exactly the code I'm using...


    "Evertjan." <> wrote in message
    news:Xns99A0B28A2C8FCeejj99@194.109.133.242...
    > Lasse Edsvik wrote on 03 sep 2007 in
    > microsoft.public.inetserver.asp.general:
    >
    > > Evertjan,

    >
    > >> What do you mean by "open a textfile" in ASP?
    > >>
    > >> Show us your code [ONLY the relevant workingpart please]

    >
    > [please do not toppost on usenet and do not quote signatures]
    >
    > > Evertjan,
    > >
    > > as a tested with:
    > >
    > > (file is saved as UTF-8 )
    > >
    > > <%
    > > Set fs=Server.CreateObject("Scripting.FileSystemObject")

    >
    > Read this:
    >
    > http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr06/hey04
    > 19.mspx
    >
    >
    > >> What do you mean by "run it" after you open it?
    > >> Can you "run" a text file, or do you mean an ASP or HTML file?

    >
    > ??
    >
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)
    Lasse Edsvik, Sep 4, 2007
    #6
  7. "Lasse Edsvik" <> wrote in message
    news:...
    > That's exactly the code I'm using...
    >



    As Martin has pointed out UTF-8 is not supported by FileSystemObject. Here
    is one way to do it with ADODB:-

    Dim oStream : oStream = Server.CreateObject("ADODB.Stream")

    oStream.Open
    oStream.LoadFromFile Server.MapPath("/files/temp/test.csv")
    oStream.CharSet = "UTF-8"

    Response.Write oStream.ReadText

    What is the client going to do with this response? Load into Excel?

    If there is a good reason for the CSV to be in UTF-8 encoding then the
    response code page also needs to be UTF-8. That being the case it may be
    better to send it as binary like this:-

    <%

    Response.ContentType = "text/csv"
    Response.CharSet = "UTF-8"

    Dim oStream : oStream = Server.CreateObject("ADODB.Stream")

    oStream.Type = 1 'Binary
    oStream.Open
    oStream.LoadFromFile Server.MapPath("/files/temp/test.csv")

    Response.BinaryWrite oStream.Read

    %>

    This avoids converting UTF-8 to Unicode only to have the response convert it
    back to UTF-8 again.

    How big is the actual file likely to be?

    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Sep 4, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John C. Bollinger
    Replies:
    0
    Views:
    5,312
    John C. Bollinger
    Jun 1, 2004
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,285
    Tim Rentsch
    Sep 23, 2005
  3. Hongyu
    Replies:
    9
    Views:
    902
    James Kanze
    Aug 8, 2008
  4. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    225
    Dan Rogers
    Nov 16, 2004
  5. Michael Tan
    Replies:
    32
    Views:
    964
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page