Here is my code ..
string attachment = "attachment; filename=Call-Details-Report-" + startDate.SelectedDate.Value.ToString("MM-dd-yyyy") + ".csv"; Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition", attachment); Response.ContentType = "text/csv"; Response.AddHeader("Pragma", "public"); Response.WriteFile(downloadLocation+"\\"+fileName); Response.End();
I am using the above code to download a csv file from a location. But itβs amazing that the contents are written twice or several times to the file that I upload, although this is actually not the case with the file on the server. I am writing my code in C #. The above code snippet works fine on the local machine, but the problem is on the Production server.
Here is my complete method
private void DownloadReport(string query) { string downloadFolderPath = ""; string filePath = ""; string dbAndApplicationServerStatus = ConfigurationManager.AppSettings["SameDBAndApplicationServer"] != null ? ConfigurationManager.AppSettings["SameDBAndApplicationServer"] : "1"; if (dbAndApplicationServerStatus == "0") { Log.Write("So the DB And Application are on differrent servers,hence trying to read Download folder path on DB Server...."); downloadFolderPath = ConfigurationManager.AppSettings["ReportDownloadLocation"] != null ? ConfigurationManager.AppSettings["ReportDownloadLocation"] : "-1"; Log.Write("Download Path is " + downloadFolderPath); } else { Log.Write("So the DB and Application and Db are on same server......"); downloadFolderPath = Server.MapPath("Download"); downloadFolderPath = downloadFolderPath.Replace("\\", "//"); if (!Directory.Exists(downloadFolderPath)) { Directory.CreateDirectory(downloadFolderPath); } Log.Write("Download Path is " + downloadFolderPath); } string status=""; StringBuilder headerQuery = new StringBuilder(); StringBuilder rowQuery = new StringBuilder(); StringBuilder sqlQuery = new StringBuilder(); filePath = downloadFolderPath; string folderName = DateTime.Now.ToString("MM-dd-yyyy"); string timeStamp = DateTime.Now.ToString("MM-dd-yy-HH-mm-ss"); string fileName = "Call-Details-Report-" + startDate.SelectedDate.Value.ToString("MM-dd-yyyy") + "_" + timeStamp + ".csv"; filePath = filePath + "/" + fileName; bool commaRequired = false; sqlQuery.Append("SELECT * INTO OUTFILE '"); sqlQuery.Append(filePath); sqlQuery.Append("' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' FROM ("); headerQuery.Append("Select "); rowQuery.Append("(Select "); #region Creating Query #endregion if (!CdrSearch.WriteReportToFile(sqlQuery.ToString(),out status)) { Log.Write("Failed to generate the file to download......"); WebPagesHelper.ShowMessage(ref lblMessage, WebPagesHelper.MessageType.Message, status); } else { Log.Write("Succesfully generated file to Download"); string downloadLocation = Server.MapPath("Download"); if (dbAndApplicationServerStatus == "0") { WebClient webClient = new WebClient(); string path = ConfigurationManager.AppSettings["DownloadURL"] != null ? ConfigurationManager.AppSettings["DownloadURL"].ToString() : ""; if (!Directory.Exists(downloadLocation)) { Directory.CreateDirectory(downloadLocation); } if (File.Exists(downloadLocation + "\\" + fileName)) { File.Delete(downloadLocation + "\\" + fileName); } webClient.DownloadFile(path + fileName, downloadLocation + "\\" + fileName); } Log.Write("Configured Download Location on Application" + downloadLocation); string attachment = "attachment; filename=Call-Details-Report-" + startDate.SelectedDate.Value.ToString("MM-dd-yyyy") + ".csv"; Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition", attachment); Response.ContentType = "text/csv"; Response.AddHeader("Pragma", "public"); Log.Write(downloadLocation + "\\" + fileName); Response.WriteFile(downloadLocation+"\\"+fileName); Response.SetCookie(new HttpCookie("DStatus", "Completed")); Response.End(); } }
And the aforementioned method is called only once to click on the button, so there are no questions about the cycle happening here.
iJade source share