private void DownloadBidFile(DataSet dsExtractData, string filename, string delimit) { try { if (txtFromDate.SelectedDate != null) { fromdate = DateTime.Parse(txtFromDate.SelectedDate.ToString()); } if (txtToDate.SelectedDate != null) { todate = DateTime.Parse(txtToDate.SelectedDate.ToString()); } string dateFormat = "dd-mm-yyyy"; dsExtractData = onlineOrderBackOfficeBo.GetBSECustomer(adviserVo.advisorId, fromdate, todate); dsExtractData.Tables[0].Columns.Remove("C_DematAcceptedOn"); StreamWriter str = new StreamWriter(Server.MapPath(@"~/UploadFiles/" + filename), false, System.Text.Encoding.Default); string Columns = string.Empty; foreach (DataColumn column in dsExtractData.Tables[0].Columns) { Columns += column.ColumnName + delimit; } // Headers For different types DataColumn[] arrCols = new DataColumn[dsExtractData.Tables[0].Columns.Count]; dsExtractData.Tables[0].Columns.CopyTo(arrCols, 0); foreach (DataRow datarow in dsExtractData.Tables[0].Rows) { string row = string.Empty; int i = 0; foreach (object item in datarow.ItemArray) { if (arrCols[i].DataType.FullName == "System.DateTime") { string strDate = string.IsNullOrEmpty(item.ToString()) ? "" : DateTime.Parse(item.ToString()).ToString(dateFormat); row += strDate + delimit; } else { row += item.ToString().Trim() + delimit; } i++; } str.WriteLine(row.Remove(row.Length - 1, 1)); } str.Flush(); str.Close(); string myFileData; // File in myFileData = File.ReadAllText(Server.MapPath("~/UploadFiles/" + filename)); // Remove last CR/LF // 1) Check that the file has CR/LF at the end if (myFileData.EndsWith(Environment.NewLine)) { //2) Remove CR/LF from the end and write back to file (new file) //File.WriteAllText(@"D:\test_backup.csv", myFileData.TrimEnd(null)); // Removes ALL white spaces from the end! File.WriteAllText(Server.MapPath("~/UploadFiles/" + filename), myFileData.TrimEnd(Environment.NewLine.ToCharArray())); // Removes ALL CR/LF from the end! } #region download notepad or text file. Response.ContentType = "Text/Plain"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename); string aaa = Server.MapPath("~/UploadFiles/" + filename); Response.TransmitFile(Server.MapPath("~/UploadFiles/" + filename)); HttpContext.Current.ApplicationInstance.CompleteRequest(); Response.End(); #endregion } catch (Exception ex) { } //finally //{ // System.IO.File.Delete(Server.MapPath("~/UploadFiles/" + filename)); //} }