Problems with special chars when outputting a text stream from a aspx filehttp://forums.asp.net/t/1055803.aspx/1?Problems+with+special+chars+when+outputting+a+text+stream+from+a+aspx+fileWed, 17 Jan 2007 05:44:40 -050010558031501831http://forums.asp.net/p/1055803/1501831.aspx/1?Problems+with+special+chars+when+outputting+a+text+stream+from+a+aspx+fileProblems with special chars when outputting a text stream from a aspx file <p><font size="3"><font face="Times New Roman">Hi all</font></font></p> <p><font face="Times New Roman" size="3">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 &quot;text/x-vCard&quot; and then send the content of the file. It all works great, unless theres 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 cant figure out how to disable it, please help.</font></p> <p><font face="Times New Roman" size="3">Ive included the code below from my Page_Load method. My .aspx is completely empty (besides the required </font><span style="font-size:10pt; background:yellow; font-family:'Courier New'">&lt;%</span><span style="font-size:10pt; color:blue; font-family:'Courier New'">@</span><span style="font-size:10pt; font-family:'Courier New'"> <span style="color:maroon">Page ... </span></span><font face="Times New Roman" size="3">directives).</font></p> <p><font face="Times New Roman" size="3">Thanks, Egil.</font></p> <p><font face="Times New Roman" size="3">-------------------------------------------</font></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Buffer response so that page is sent</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// after processing is complete.</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.BufferOutput = <span style="color:blue">true</span>;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Surpress the HTTP Content-Type header</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.Charset = <span style="color:blue">null</span>; <span style="color:green">// &quot;utf-8&quot;; //&quot;iso-8859-2&quot;;</span></span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.HeaderEncoding = Encoding.UTF8;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.ContentEncoding = Encoding.UTF8;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Clear response stream</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.Clear();</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Set the response mime type for vCard</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.ContentType = <span style="color:maroon">&quot;text/x-vCard&quot;</span>;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Build output</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">StringBuilder sb = <span style="color:blue">new</span> StringBuilder();</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Add headers</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">sb.AppendLine(<span style="color:maroon">&quot;BEGIN:VCARD&quot;</span>);</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">sb.AppendLine(<span style="color:maroon">&quot;VERSION:2.1&quot;</span>);</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Add content</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// last and first name</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">sb.AppendLine(String.Format(<span style="color:maroon">&quot;N:{0};{1}&quot;</span>, <span style="color:maroon">&quot;Some last name&quot;</span>, <span style="color:maroon">&quot;Some first name&quot;</span>));</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// full name</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">sb.AppendLine(String.Format(<span style="color:maroon">&quot;FN:{0} {1}&quot;</span>, <span style="color:maroon">&quot;Some first name&quot;</span>, <span style="color:maroon">&quot;Some last name&quot;</span>).Trim());</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// title</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:blue; font-family:'Courier New'">if</span><span style="font-size:10pt; font-family:'Courier New'">(!String.IsNullOrEmpty(p[<span style="color:maroon">&quot;title&quot;</span>])) {</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>sb.AppendLine(String.Format(<span style="color:maroon">&quot;TITLE:{0}&quot;</span>, <span style="color:maroon">&quot;Some title&quot;</span>));</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">}</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// phone</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:blue; font-family:'Courier New'">if</span><span style="font-size:10pt; font-family:'Courier New'">(!String.IsNullOrEmpty(p[<span style="color:maroon">&quot;telephoneNumber&quot;</span>])) {</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>sb.AppendLine(String.Format(<span style="color:maroon">&quot;TEL;PREF:{0}&quot;</span>, <span style="color:maroon">&quot;Some phone number&quot;</span>));</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">}</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// mobile</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:blue; font-family:'Courier New'">if</span><span style="font-size:10pt; font-family:'Courier New'">(!String.IsNullOrEmpty(p[<span style="color:maroon">&quot;mobile&quot;</span>])) {</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>sb.AppendLine(String.Format(<span style="color:maroon">&quot;TEL;CELL;VOICE:{0}&quot;</span>, <span style="color:maroon">&quot;Some mobile number&quot;</span>));</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">}</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// mail</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:blue; font-family:'Courier New'">if</span><span style="font-size:10pt; font-family:'Courier New'">(!String.IsNullOrEmpty(p[<span style="color:maroon">&quot;mail&quot;</span>])) {</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>sb.AppendLine(String.Format(<span style="color:maroon">&quot;EMAIL;PREF;INTERNET:{0}&quot;</span>, <span style="color:maroon">&quot;Some email&quot;</span>));</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">}</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// add footers</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">sb.AppendLine(<span style="color:maroon">&quot;END:VCARD&quot;</span>);</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Add string to output</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.Write(sb.ToString());</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">&nbsp;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:green; font-family:'Courier New'">// Send all currently buffered output to the client and stops execution of the page.</span></p> <p><span style="font-size:10pt; font-family:'Courier New'">Response.End();</span></p> <p><font face="Times New Roman" size="3">&nbsp;</font></p> 2006-12-19T12:40:17-05:001502477http://forums.asp.net/p/1055803/1502477.aspx/1?Re+Problems+with+special+chars+when+outputting+a+text+stream+from+a+aspx+fileRe: Problems with special chars when outputting a text stream from a aspx file You could try Response.BinaryWrite instead of Response.Write and see what happens. 2006-12-19T19:43:17-05:001535617http://forums.asp.net/p/1055803/1535617.aspx/1?Re+Problems+with+special+chars+when+outputting+a+text+stream+from+a+aspx+fileRe: Problems with special chars when outputting a text stream from a aspx file <p><font face="Times New Roman" color="#0000ff" size="3"><font face="arial,helvetica,sans-serif" color="#000000">Hi, </font></font></p> <p>Whatever I&nbsp;comment the following two line or not, the&nbsp;ouput vcard.vcf is UTF-8 encoded. &nbsp;</p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.HeaderEncoding = Encoding.UTF8;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">Response.ContentEncoding = Encoding.UTF8;</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"></span>&nbsp;</p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><font face="arial,helvetica,sans-serif">I've add a Chinese word in title.&nbsp;I doesn't make any other&nbsp;modification except&nbsp;comment the line as following since varialbe p is not accessible here.</font></span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"></span>&nbsp;</p> <span style="font-size:10pt; font-family:'Courier New'"> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; color:blue; font-family:'Courier New'">//if</span><span style="font-size:10pt; font-family:'Courier New'">(!String.IsNullOrEmpty(p[<span style="color:maroon">&quot;telephoneNumber&quot;</span>])) {</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><span style="">&nbsp;&nbsp;&nbsp; </span>sb.AppendLine(String.Format(<span style="color:maroon">&quot;TEL;PREF:{0}&quot;</span>, <span style="color:maroon">&quot;Some phone number&quot;</span>));</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'">//}</span></p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"></span>&nbsp;</p> <p class="MsoNormal" style="margin:0cm 0cm 0pt"><span style="font-size:10pt; font-family:'Courier New'"><font face="arial,helvetica,sans-serif">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.</font></span></p> </span> <p><font face="Times New Roman" color="#0000ff" size="3">BEGIN:VCARD<br> VERSION:2.1<br> N:Some last name;Some first name<br> FN:Some first name Some last name<br> TITLE:Some title <br> TEL;PREF:Some phone number<br> TEL;CELL;VOICE:Some mobile number<br> EMAIL;PREF;INTERNET:Some email<br> END:VCARD</font></p> <p><font face="arial,helvetica,sans-serif" color="#000000"></font>&nbsp;</p> 2007-01-17T05:44:40-05:00