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).
EgilOfBorg
Member
173 Points
49 Posts
Problems with special chars when outputting a text stream from a aspx file
Dec 19, 2006 12:40 PM|LINK
Hi all
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
// after processing is complete.
Response.BufferOutput = true;
// Surpress the HTTP Content-Type header
Response.Charset = null; // "utf-8"; //"iso-8859-2";
Response.HeaderEncoding = Encoding.UTF8;
Response.ContentEncoding = Encoding.UTF8;
// Clear response stream
Response.Clear();
// Set the response mime type for vCard
Response.ContentType = "text/x-vCard";
// Build output
StringBuilder sb = new StringBuilder();
// Add headers
sb.AppendLine("BEGIN:VCARD");
sb.AppendLine("VERSION:2.1");
// Add content
// last and first name
sb.AppendLine(String.Format("N:{0};{1}", "Some last name", "Some first name"));
// full name
sb.AppendLine(String.Format("FN:{0} {1}", "Some first name", "Some last name").Trim());
// title
if(!String.IsNullOrEmpty(p["title"])) {
sb.AppendLine(String.Format("TITLE:{0}", "Some title"));
}
// phone
if(!String.IsNullOrEmpty(p["telephoneNumber"])) {
sb.AppendLine(String.Format("TEL;PREF:{0}", "Some phone number"));
}
// mobile
if(!String.IsNullOrEmpty(p["mobile"])) {
sb.AppendLine(String.Format("TEL;CELL;VOICE:{0}", "Some mobile number"));
}
// mail
if(!String.IsNullOrEmpty(p["mail"])) {
sb.AppendLine(String.Format("EMAIL;PREF;INTERNET:{0}", "Some email"));
}
// add footers
sb.AppendLine("END:VCARD");
// Add string to output
Response.Write(sb.ToString());
// Send all currently buffered output to the client and stops execution of the page.
Response.End();
encoding