0

I have an endpoint that receives a spreadsheet and call a service to read and process this spreadsheet. And i would like to return this spreadsheet if I find some inconsistency or error (with some correction indications).

But how can I do to return this file through a controller action? I know that I can use the function File( ) = FileContentResult, but I would like to return this with a status code that indicates an error operation, like BadRequest(400) or Conflict(409).

I've tried to use the File( ) function with both status code, but the downloaded file gets corrupted and I can't open it.

    [HttpPost]
    [Consumes("multipart/form-data")]
    public async Task<ActionResult> Upload([FromForm] UploadSpreadsheetCommand command)
    {
        var response = await _mediator.Send(command);

        if (response.IsValid)
            return Ok();

        Response.StatusCode = StatusCodes.Status409Conflict;

        return File(response.Data.SpreadsheetError, "application/ms-excel", "errors.xlsx");
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
FeGo
  • 1
  • FYI, by default [`ControllerBase.File()`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.file?view=aspnetcore-7.0) returns 200. For my perspective, it is either that you return error code: 400 or others (to just indicate the process is failed) or just return `File()` with default status code as 200, but not return file and status code with error code at the same time. – Yong Shun May 16 '23 at 01:51
  • try changing the MIME type tp "application/vnd.ms-excel" – Nate1zn May 16 '23 at 06:45

0 Answers0