Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Wednesday, 7 December 2011

ASP.NET MVC using fileresult to export data in unicode

Exporting data to a file is a common feature websites provides to its customers. Now consider if the website is used by customers around the world, then this export data should support localization and globalization. Here is the snippet of code relating to WriteFile() for exporting data:-
protected override void WriteFile(HttpResponseBase response)
{
            response.Clear();
            response.ClearContent();
            response.ContentEncoding = System.Text.Encoding.Unicode;
            StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.Default);
            writer.WriteLine("Test Data");
            writer.Flush();
}
Since "response.ContentEncoding = System.Text.Encoding.Unicode;" is assigned to correct encoding browse should enforce writing data in unicode format but point to remember is streamwriter is initialised with encoding as "Encoding.Default". So browse ignores content encoding. Modify your code to have
StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.Unicode);
Now this will enforce unicode format of the file. Also to verify if the file format is correct, rename the file to .bin and open Visual Studio to see the file in hex format. Verify the Byte Order Mark (BOM) of the file. First four char's "FF FE" confirms the file to be in unicode format. Refer to this article UTF-8, UTF-16, UTF-32 & BOM for more details about BOM.

Never trust anything that can think for itself if you can't see where it keeps its brain. ~J.K. Rowling

4 comments:

Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers