2012-06-26 73 views
4

這裏是我的代碼..Response.WriteFile內容寫入兩次

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(); 

我用上面的代碼從location.But下載一個CSV文件出奇的內容被寫入兩次或有時三次進入我下載的文件,但實際上它不是這樣的服務器上的文件。我正在用c#編寫我的代碼。 上面這段代碼在本地機器上完美工作,但問題在於生產服務器。

這裏是我的完整方法

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(); 
    } 
} 

和上面的方法被稱爲只有一次直線距離上的一個按鈕的點擊,所以這裏的循環沒有任何問題。

+3

檢查是否有多次調用此正文的方法。我認爲這是... –

+5

我認爲你已經顯示了錯誤的代碼片段。這是將文件寫入'downloadLocation'的代碼,可能有問題。 –

+0

@DarinDimitrov Umm no它將指定文件的內容直接寫入HTTP響應輸出流作爲文件塊。 – carny666

回答

0

確定所以真正的罪魁禍首是在這種情況下的Response.WriteFile。在我的情況下,我猜測因爲數據的大小是相當巨大的Response.WriteFile沒有按預期工作。我發現一些情況下,大文件下載,其最好使用Response.TransmitFile.Left與沒有其他選項,我改變了我的代碼,並使用Response.TransmitFile和尤里卡!問題解決了。在下載的文件中沒有更多的重複記錄。儘管原因還不清楚,但Response.TransmitFile解決了這個問題.....

0

您可以檢查一下以下行意味着:

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

Response.WriteFile(downloadLocation+"\\"+fileName); 

註釋掉其中之一,如果他們真的做同樣給一試。 作爲一項安全措施,請在下載完成之前禁用該按鈕。

當我嘗試下面的代碼(甚至發佈在IIS上),它只是下載一次,如預期的那樣。

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

well webclient用於從數據庫服務器下載數據到應用程序服務器和Response.WriteFile用於從應用程序服務器下載到客戶端機器 – iJade

0

顯然有什麼奇怪的事情發生。你說它可以在開發中使用,但不是產品 - 你是否在兩個環境中使用相同的服務器配置(即,你在開發中使用1個服務器,但在產品中使用2個服務器?)

您可能有3個步驟,假設我理解你的代碼...

  • 從SQL生成報告。如果該文件存儲在不同的服務器上寫入文件
  • ,將其下載到Web服務器
  • 即成它

因此,在更復雜的情況下(我假設生產是),在這個過程中的哪一步你開始看到重複的條目?在服務器上,報告是在Web服務器的副本上生成的,還是僅在客戶端從Web服務器獲取後生成的?

我看不出爲什麼你的代碼將文件提供給客戶端的任何原因都會複製數據,因此只能假設它在某個時刻出現在那裏。

如果你可以使用Firebug/Fiddler /?從網絡服務器發送到客戶端的確切內容

(順便提一下,您可能想要查看System.IO.Path類來處理路徑,它使您的代碼更具可讀性和健壯性 - 不必擔心後面的斜線! )

+0

數據重複是在第三步.....我認爲罪魁禍首是Response.Wri teFile,因爲我的文件是相當大的.......我正在測試與Response.TransmitFile上prod ....將保持張貼 – iJade

+0

@jade你有沒有考慮轉入步進.Net框架源代碼,看看什麼它在'Response.WriteFile();'中做什麼? – Basic

+0

它是一個庫我猜,我怎麼能進入Response.WriteFile? – iJade