2015-02-23 181 views
-2

即時通訊使用下面的代碼通過http請求發送一個csv文件。代碼運行良好,但文件不下載客戶端機器上,不保存爲菜單,什麼都沒有出現。我用於另一個報告的相同代碼,它工作正常,文件也被下載。我在本地主機上運行項目。但不是按鈕btnGenerateReport。csv文件沒有從asp.net的鉻瀏覽器下載c#app

protected void btnGenerateReport_Click(object sender, EventArgs e) 
    { 
     DataTable dt = new DataTable(); 
     MHSProgressNotesBL oMHSProgressNote = new MHSProgressNotesBL(); 
     int ClientId = Convert.ToInt32(ddlClients.SelectedValue); 
     int LocationId = Convert.ToInt32(ddlLocations.SelectedValue); 
     int PractitionerId = Convert.ToInt32(ddlPractitioner.SelectedValue); 

     dt = oMHSProgressNote.FetchBillingReport(LocationId, ClientId, PractitionerId, Convert.ToDateTime(txtNoteStartDate.Text), Convert.ToDateTime(txtNoteEndDate.Text)); 
     WriteToCSV(dt); 

    } 

    private void WriteToCSV(DataTable dt) 
    { 
     try 
     { 
      string attachment = "attachment; filename=ClientRoster.csv"; 
      HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.ClearHeaders(); 
      HttpContext.Current.Response.ClearContent(); 
      HttpContext.Current.Response.AddHeader("content-disposition", attachment); 
      HttpContext.Current.Response.ContentType = "text/csv"; 
      //HttpContext.Current.Response.AddHeader("Pragma", "public"); 
      WriteColumnName(dt); 
      foreach (DataRow clientdata in dt.Rows) 
      { 
       WriteUserInfo(clientdata); 
      } 
      HttpContext.Current.Response.End(); 
      //HttpContext.Current.ApplicationInstance.CompleteRequest(); 
     } 
     catch (Exception e) 
     { } 

    } 

    private void WriteColumnName(DataTable dt) 
    { 
     string columnNames = ""; 
     // Write Header row 
     foreach (DataColumn item in dt.Columns) 
     { 
      if (item.ColumnName.Contains(',')) 
      { 
       columnNames += "\"" + item.ColumnName + "\""; 
      } 
      else 
      { 
       columnNames += item.ColumnName + ","; 
      } 
     } 
     columnNames = columnNames.TrimEnd(','); 

     HttpContext.Current.Response.Write(columnNames); 
     HttpContext.Current.Response.Write(Environment.NewLine); 
    } 

    private void WriteUserInfo(DataRow clientDataRow) 
    { 
     StringBuilder stringBuilder = new StringBuilder(); 

     foreach (var item in clientDataRow.ItemArray) 
     { 
      AddComma((string)(item == System.DBNull.Value ? "" : item.ToString()), stringBuilder); 
     } 
     HttpContext.Current.Response.Write(stringBuilder.ToString().TrimEnd(',')); 
     HttpContext.Current.Response.Write(Environment.NewLine); 

    } 

    private void AddComma(string value, StringBuilder stringBuilder) 
    { 
     if (value.Contains('"')) 
     { 
      value = value.Replace(@"""", @""""""); 
      value = "\"" + value + "\""; 
     } 
     else if (value.Contains(',')) 
     { 
      value = "\"" + value + "\""; 
     } 
     stringBuilder.Append(value); 
     stringBuilder.Append(","); 
    } 
+0

你確實需要重構代碼。它的可維護性差,並且可能會導致問題 - 您應該有一個生成CSV輸出或文件的函數,然後讓調用者寫入響應,因此所有內容都易於管理,而不是寫入響應流的不同方法。這也將使您更容易針對您遇到和遇到的問題。 – 2015-02-24 09:53:07

回答

0

的代碼是確定.........的主要問題是該按鈕btnGenerateReport是一個更新面板。由於其誤差在上升的內:從所述服務器接收的消息無法被解析。此錯誤的常見原因是,通過調用Response.Write(),響應篩選器,HttpModules或服務器跟蹤已啓用來修改響應時。 詳細信息:在「客戶,醫師」附近解析錯誤。 將按鈕放置在更新面板之外後,代碼正常工作。

+0

是的,確切地說。你不能寫入你有更新面板的區域。 – 2015-02-24 09:54:14