I have a form which will return a csv file
whenever user click a button. In order to return file I have created a StringBuilder
named sb
and concatenate all responses to sb
(a point to mention is that my strings are Arabic characters so I need to encode them as UTF8 chars). Then I try to return my csv file like this:
DataTable dt = auc.GetTradesByDate().Tables[0];
/**************************************************************/
StringBuilder sb = new StringBuilder();
sb.Append("date, symbol, symbolName, buyerId,buyerName\n");
foreach (DataRow dr in dt.Rows)
{
sb.Append(dr["date"]);
sb.Append("," + dr["symbol"]);
sb.Append("," + dr["symbolName"]);
sb.Append("," + dr["buyerId"]);
sb.Append("," + dr["buyerName"]);
sb.Append("\n");
}
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/csv";
Response.Charset = "";
Response.ContentEncoding = Encoding.UTF8;
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;filename=trades_{0:yyyy-MM-dd}.csv", DateTime.Now));
byte[] BOM = new byte[] { 0xef, 0xbb, 0xbf };
Response.BinaryWrite(BOM);
Response.BinaryWrite(Encoding.UTF8.GetBytes(sb.ToString()));
Response.Flush();
Response.End();
The sb has filled correctly but the output file is still empty. I will appreciate your helps. can anybody help me with that?
I have used all the methods proposed(1 and 2) but it's still empty. and The point is that when I write string to XML file everything works fine but when it comes to CSV it doesn't work. That's really making me crazy. I don't know what's the problem. am I forgetting to write anything??