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ファイルをダウンロードしていますが、実際にはサーバー上のファイルではそうではありませんが、ダウンロードしたファイルにコンテンツが2回または3回書き込まれます。 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();
    }
}

また、上記のメソッドはボタンをクリックするだけですぐに1回だけ呼び出されるため、ここでループする問題はありません。

4

3 に答える 3

0

次の行の意味を確認できます。

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

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

彼らが本当に同じことをしているなら、それらの1つをコメントアウトして試してみてください。安全対策として、ダウンロードが完了するまでボタンを無効にしてください。

次のコード(IISで公開されている場合でも)を試したところ、予想どおり、ダウンロードは1回だけです。

 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.WriteFileが期待どおりに機能していなかったと思います。大きなファイルをダウンロードする場合は、他のオプションなしでResponse.TransmitFile.Leftを使用します。コードを変更し、Response.TransmitFileとeurekaを使用しました。問題は解決しました。ダウンロードしたファイルに重複するレコードはありません。理由はまだ不明ですが、Response.TransmitFileが問題を解決しました。

于 2012-06-28T18:48:34.300 に答える
0

明らかに何かひねくれたことが起こっています。devでは機能するがProdでは機能しないとおっしゃいました-両方の環境で同じサーバー構成を使用していますか(つまり、devで1台のサーバーを使用していますが、prodで2台のサーバーを使用していますか?)

私があなたのコードを理解したと仮定すると、あなたは潜在的に3つのステップを持っています...

  • SQLからレポートを生成し、ファイルに書き込みます
  • ファイルが別のサーバーに保存されている場合は、Webサーバーにダウンロードします
  • それを提供します

それで、より複雑なシナリオ(私はプロダクションであると仮定します)では、プロセスのどのステップでダブルエントリが表示され始めますか?レポートは、サーバー上で、Webサーバーのコピー上で、またはWebサーバーからレポートを取得した後のクライアントでのみ生成されますか?

ファイルをクライアントに提供するためのコードがデータを複製する理由がわからないため、ある時点で発生する前にデータが複製されていると想定することしかできません。

Firebug / Fiddler/???を使用できると便利です。Webサーバーからクライアントへの送信の正確な内容を投稿する

(ちなみに、System.IO.Pathパスを操作するためのクラスを確認することをお勧めします。これにより、コードが読みやすく堅牢になります。末尾のスラッシュについて心配する必要はありません!)

于 2012-06-27T11:41:26.140 に答える