Wednesday, May 25, 2011

Download CSV file and solving the encoding problem

I will not talk too much ..
Most of the cases you open the blog for a fast review to get an answer and if you did not find it within seconds, you will leave :)
So here it is

    Response.Clear();
string fileName= String.Format("Client_{0}.csv", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "filename=" + fileName);
Response.Write("Ahmed,Mohamed,Adel\n");
Response.End();
EDIT : 30/05/2010
Solving the problem with UTF-8
CSV file has a problem with UTF-8 encoding.
To solve this problem, you have to know first what is BOM - If you do not care you can move down to the end of the post and get the solution directly but for those who want to learn more, you can continue reading-. BOM is Byte Order Mask which is a Unicode character used to signal the byte order of a text file or stream. Its code is U+EEFF. BOM use is optional and if used should appear at the start of the text stream. Beyond its specific use as a byte-order indicator, the BOM character may also indicate which of the several Unicode representations the text is encoded in.

The UTF-8 representation of the BOM is the byte sequence 0xEF, 0xBB, 0xBF.

So the code I wrote is :

Response.Clear();
string fileName = String.Format("Requests_{0}.csv",DateTime.Now.ToString("yyyyMMdd"));
Response.ContentType = "application/csv";
Response.AddHeader("Content-Type", "text/csv");
Response.AddHeader("content-disposition", "filename=" + fileName);
Response.Charset = Encoding.UTF8.WebName;
Response.HeaderEncoding = Encoding.UTF8;
Response.ContentEncoding = Encoding.UTF8;
byte[] bytes = new UTF8Encoding().GetBytes(requestsBuilder.ToString());
List bytesList = new List();
bytesList.Add((Byte)239);
bytesList.Add((Byte)187);
bytesList.Add((Byte)191);
bytesList.AddRange(bytes);
Response.BinaryWrite(bytesList.ToArray());
Response.Flush();
Response.End();

It works more than perfect :)
Hope you enjoy it

Monday, May 23, 2011

PostbackUrl vs NavigateUrl

Difference between PostbackUrl and NavigateUrl

NavigateURL: Submits a 'Get' request to the URL (just like if you had typed it in your browser)

PostBackURL: Submits a 'Post' request containing the information from the form to the specified URL.

Thursday, May 19, 2011

ASP Menu: Above Extra Space on Chrome Browser


STORY

While working on a project, I tried to remove the extra space above an asp menu which is visible on Google Chrome browser ,but not on IE.

I did not find any padding or margins that cause that !!


To find the reason I view the output html source and I find that there were an image above the control !!

<img alt="Skip Navigation Links" src="/WebResource.axd?d=m8HuaqPYLmzk_UowATGkDX-O9aChdqOof5uTooTVtSHcyrMcV8ntTntRgD92n1YaI9VBs2mZBYD1JR0kt803GUDBQtemqa3fUePbK6F-lHA1&amp;t=634224162214550464" width="0" height="0" style="border-width:0px;">

When I removed this image, the space is remove, so what is the reason for it ?!

FIX

There is an easy fix to it by setting the SkipLinkText property of the menu equal to ”” (empty string) will stop the menu control from rendering the accessibility feature and Safari, Chrome and IPhone’s and IPod’s mobile Safari browsers (my headache) will stop adding that 15px on the top of the menu.

Hope it helps