locked
Problems with special chars when outputting a text stream from a aspx file RRS feed

  • Question

  • User1540266618 posted

    Hi all<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    I want to allow people to download vCards that I genereate on the fly. Since vCards is basically just a specially formatted text file, it's easy to read the required data from a database and simply change the Response.ContentType to "text/x-vCard" and then send the content of the file. It all works great, unless there’s a special chars in there like Ø, Æ and Å or similar. The content looks right up until I do a Response.Write(sb.ToString()), but when I open the file on my computer after it's finished downloading, the special chars are looking weird. So my guess is that the Response object does something to the encoding, but I can’t figure out how to disable it, please help.

    I’ve included the code below from my Page_Load method. My .aspx is completely empty (besides the required <%@ Page ... directives).

    Thanks, Egil.

    -------------------------------------------

    // Buffer response so that page is sent<o:p></o:p>

    // after processing is complete.<o:p></o:p>

    Response.BufferOutput = true;<o:p></o:p>

    <o:p> </o:p>

    // Surpress the HTTP Content-Type header<o:p></o:p>

    Response.Charset = null; // "utf-8"; //"iso-8859-2";<o:p></o:p>

    Response.HeaderEncoding = Encoding.UTF8;<o:p></o:p>

    Response.ContentEncoding = Encoding.UTF8;<o:p></o:p>

    <o:p> </o:p>

    // Clear response stream<o:p></o:p>

    Response.Clear();<o:p></o:p>

    <o:p> </o:p>

    // Set the response mime type for vCard<o:p></o:p>

    Response.ContentType = "text/x-vCard";<o:p></o:p>

    <o:p> </o:p>

    // Build output<o:p></o:p>

    StringBuilder sb = new StringBuilder();<o:p></o:p>

    <o:p> </o:p>

    // Add headers<o:p></o:p>

    sb.AppendLine("BEGIN:VCARD");<o:p></o:p>

    sb.AppendLine("VERSION:2.1");<o:p></o:p>

    <o:p> </o:p>

    // Add content<o:p></o:p>

    // last and first name<o:p></o:p>

    sb.AppendLine(String.Format("N:{0};{1}", "Some last name", "Some first name"));<o:p></o:p>

    <o:p> </o:p>

    // full name<o:p></o:p>

    sb.AppendLine(String.Format("FN:{0} {1}", "Some first name", "Some last name").Trim());<o:p></o:p>

    <o:p> </o:p>

    // title<o:p></o:p>

    if(!String.IsNullOrEmpty(p["title"])) {<o:p></o:p>

        sb.AppendLine(String.Format("TITLE:{0}", "Some title"));<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    // phone<o:p></o:p>

    if(!String.IsNullOrEmpty(p["telephoneNumber"])) {<o:p></o:p>

        sb.AppendLine(String.Format("TEL;PREF:{0}", "Some phone number"));<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    // mobile<o:p></o:p>

    if(!String.IsNullOrEmpty(p["mobile"])) {<o:p></o:p>

        sb.AppendLine(String.Format("TEL;CELL;VOICE:{0}", "Some mobile number"));<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    // mail<o:p></o:p>

    if(!String.IsNullOrEmpty(p["mail"])) {<o:p></o:p>

        sb.AppendLine(String.Format("EMAIL;PREF;INTERNET:{0}", "Some email"));<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    // add footers<o:p></o:p>

    sb.AppendLine("END:VCARD");<o:p></o:p>

    <o:p> </o:p>

    // Add string to output<o:p></o:p>

    Response.Write(sb.ToString());<o:p></o:p>

    <o:p> </o:p>

    // Send all currently buffered output to the client and stops execution of the page.<o:p></o:p>

    Response.End();

    <o:p> </o:p>

    Tuesday, December 19, 2006 8:40 AM

Answers

  • User113421904 posted

    Hi,

    Whatever I comment the following two line or not, the ouput vcard.vcf is UTF-8 encoded.  

    Response.HeaderEncoding = Encoding.UTF8;

    Response.ContentEncoding = Encoding.UTF8;

     

    I've add a Chinese word in title. I doesn't make any other modification except comment the line as following since varialbe p is not accessible here.

     

    //if(!String.IsNullOrEmpty(p["telephoneNumber"])) {

        sb.AppendLine(String.Format("TEL;PREF:{0}", "Some phone number"));

    //}

     

    The output vcard is as following, you can try open it with MS Word, it will automatically select the encoding and point out it is exactly UTF-8.

    BEGIN:VCARD
    VERSION:2.1
    N:Some last name;Some first name
    FN:Some first name Some last name
    TITLE:Some title 中文
    TEL;PREF:Some phone number
    TEL;CELL;VOICE:Some mobile number
    EMAIL;PREF;INTERNET:Some email
    END:VCARD

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 17, 2007 1:44 AM

All replies

  • User371668647 posted
    You could try Response.BinaryWrite instead of Response.Write and see what happens.
    Tuesday, December 19, 2006 3:43 PM
  • User113421904 posted

    Hi,

    Whatever I comment the following two line or not, the ouput vcard.vcf is UTF-8 encoded.  

    Response.HeaderEncoding = Encoding.UTF8;

    Response.ContentEncoding = Encoding.UTF8;

     

    I've add a Chinese word in title. I doesn't make any other modification except comment the line as following since varialbe p is not accessible here.

     

    //if(!String.IsNullOrEmpty(p["telephoneNumber"])) {

        sb.AppendLine(String.Format("TEL;PREF:{0}", "Some phone number"));

    //}

     

    The output vcard is as following, you can try open it with MS Word, it will automatically select the encoding and point out it is exactly UTF-8.

    BEGIN:VCARD
    VERSION:2.1
    N:Some last name;Some first name
    FN:Some first name Some last name
    TITLE:Some title 中文
    TEL;PREF:Some phone number
    TEL;CELL;VOICE:Some mobile number
    EMAIL;PREF;INTERNET:Some email
    END:VCARD

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 17, 2007 1:44 AM