0

我有几个 Web 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

4

1 回答 1

0

API 完全没有错误。问题在于 API 的调用和执行方式。

于 2017-12-13T22:00:29.067 回答