Response.WriteFile writes content twice

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 /*Sql Query is Created in this region*/ #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.

+4
source share
3 answers

OK, so the real culprit was Response.WriteFile in this case. In my case, I assume that the data size was quite large. Response.WriteFile did not work properly. I found some where this is in case of large file downloads, it is best to use Response.TransmitFile.Left, having no other option, I changed my code and used Response.TransmitFile and eureka! the problem was solved. More duplicate entries in the uploaded file. Although the reason is still unknown, Response.TransmitFile solved the problem .....

0
source

You can check what the following lines are for:

 webClient.DownloadFile(path + fileName, downloadLocation + "\\" + fileName); Response.WriteFile(downloadLocation+"\\"+fileName); 

Try commenting on one of them if they really do the same. As a safe measure, disable the button until the download is complete.

When I tried the following code (even published in IIS), it just loads once, as expected.

  protected void Button1_Click(object sender, EventArgs e) { string attachment = "attachment; filename=Call-Details-Report-" + DateTime.Now.ToString("MM-dd-yyyy") + ".txt"; Response.ContentType = "text/html"; Response.AddHeader("Content-Disposition", attachment); Response.AddHeader("Pragma", "public"); Response.WriteFile(@"C:\test.txt"); Response.SetCookie(new HttpCookie("DStatus", "Completed")); Response.End(); } 
0
source

There is clearly something strange there. You said that it works in dev, but not in Prod. Do you use the same server configuration in both environments (i.e. do you use 1 server in dev, but 2 in prod?)

You have 3 steps, assuming I understood your code ...

  • Create a report from SQL and write it to a file
  • If the file is stored on another server, upload it to the web server
  • Serve

So, in a more complex scenario (which I assume is Production). At what stage in the process do you start to see double entries? Is a report created on the server, on a copy of the web servers, or only on the client after it is received from the web server?

I don’t see the reasons why your code for working with a file for the client will duplicate data, so we can assume that this will happen before it appears at some point.

It would be helpful if you could use Firebug / Fiddler / ??? to send the exact contents of the transfer from the web server to the client

(By the way, you might want to take a look at the System.IO.Path class for path management, this will make your code more readable and reliable - no more worrying about trailing slashes!)

0
source

All Articles