4

这是我的代码..

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

我正在使用上面的代码从一个位置下载一个 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();
    }
}

并且上面的方法只在单击按钮时立即调用一次,所以这里没有任何循环的问题。

4

3 回答 3

0

显然发生了一些奇怪的事情。您已经说过它在开发中有效但在生产中无效 - 您在两个环境中使用相同的服务器配置(即您在开发中使用 1 个服务器但在生产中使用 2 个服务器?)

假设我已经理解了您的代码,您可能有 3 个步骤...

  • 从 SQL 生成报告并将其写入文件
  • 如果文件存储在不同的服务器上,请将其下载到 Web 服务器
  • 服务它

所以,在更复杂的场景中(我假设 Production 是),在流程的哪个步骤,您开始看到重复输入?在服务器上生成报告,在 Web 服务器的副本上,还是在从 Web 服务器获取报告后仅在客户端中?

我看不出有任何理由说明您将文件提供给客户端的代码会复制数据,因此只能假设它在某个时间点之前发生过。

如果您可以使用 Firebug/Fiddler/ 会很有帮助?发布从网络服务器到客户端的传输的确切内容

(顺便说一句,您可能想查看System.IO.Path用于操作路径的类,它使您的代码更具可读性和健壮性 - 不再担心尾部斜杠!)

于 2012-06-27T11:41:26.140 回答
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();
        }
于 2012-06-27T10:52:23.280 回答
0

好的,所以真正的罪魁祸首是 Response.WriteFile 在这种情况下。在我的情况下,我猜是因为数据的大小非常大使用 Response.TransmitFile.Left 没有其他选项我更改了我的代码并使用了 Response.TransmitFile 和尤里卡!问题已解决。下载文件中不再有重复记录。虽然原因仍然未知,但 Response.TransmitFile 解决了问题.....

于 2012-06-28T18:48:34.300 回答