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");
}