2017-10-11 47 views
0

我有一對夫婦的網頁API控制器的功能,看起來像下面的.NET Web API - ArrayBuffer亂用其他的API調用

[HttpGet] 
    [Route("GetCycle")] 
    public HttpResponseMessage GetCycle(string type) { 
     try 
     { 
      Cycle oCycleClass = Data.GetCycle(type); 
      return Request.CreateResponse(oCycleClass); 

     } 
     catch (Exception ex) 
     { 
      return Request.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, ex.Message); 
     } 

    } 

這是另一個控制器如預期那樣工作。

[HttpPost] 
    [Route("GetDataDownload")] 
    public HttpResponseMessage GetDataExport([FromBody]DataObject objectData) 
    { 
     try 
     { 
      byte[] excelData = Data.GetDataExport(objectData); 

      String timeStamp = DateTime.Now.ToString("yyyy-MM-dd HH mm ss"); 

      HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); 
      response.Content = new ByteArrayContent(excelData); 
      response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
      response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
      response.Content.Headers.ContentDisposition.FileName = "DataExport - " + focalData.User.ID + " - " + timeStamp + ".xlsx"; 

      return response; 
     } 
     catch (Exception ex) 
     { 
      return Request.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, ex.Message); 
     } 
    } 

的錯誤出現後,我執行GetDataExport功能後,任何其他控制器。它看起來像HttpResponseMessage得到某種程度上損壞。

例如:

1.執行GetCycle - 結果:成功

2.執行GetDataExport - 結果:成功

3.執行GetCycle - 結果:失敗


GetDataExport功能如下

public static byte[] GetDataExport(DataObject dataObject) 
    { 
     DataTable view = Common.ConvertToDataTable<View>(dataObject.Views); 
     string filter = Common.ConvertFiltersToSQLStatement(dataObject.Filters); 

     DataSet ds; 

     if (dataObject.DownloadWithFilters) 
     { 
      ds = InterfaceDataClass.GetEmployees(dataObject.CycleId, view, filter, 1, 0, true); 
     } 
     else 
     { 
      ds = InterfaceDataClass.GetEmployees(dataObject.CycleId, view, string.Empty, 1, 0, true); 
     } 

     using (ExcelPackage pck = new ExcelPackage()) 
     { 
      //Create the worksheet 
      ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Employees"); 

      //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1 
      ws.Cells["A1"].LoadFromDataTable(ds.Tables[0], true); 

      //Format the header column 
      using (ExcelRange rng = ws.Cells["A1:BB1"]) 
      { 
       rng.Style.Fill.PatternType = ExcelFillStyle.Solid; 
       rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(122, 122, 122)); 
       rng.Style.Font.Color.SetColor(Color.WhiteSmoke); 
      } 

      return pck.GetAsByteArray(); 
     } 
    } 

ExcelPackage來自一個庫調用EPPlus

+0

是否有任何錯誤記錄?另外,請確保您仔細關閉資源。 –

+0

什麼是關閉資源的正確方法? –

+0

我想你正在訪問一些文件流,請在那種情況下關閉它。此外,如果您可以共享代碼的GetDataExport方法,這將是有益的 –

回答

0

的API沒有錯誤可言。問題在於如何調用和執行API。