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).
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
Zhao Ji Ma
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
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
stiletto
All-Star
16995 Points
3304 Posts
Re: Problems with special chars when outputting a text stream from a aspx file
Dec 19, 2006 07:43 PM|LINK
Zhao Ji Ma -...
All-Star
23104 Points
2380 Posts
Re: Problems with special chars when outputting a text stream from a aspx file
Jan 17, 2007 05:44 AM|LINK
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
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”