'Upload multiple files from Flutter app to Net 5 server returns 400 request

I keep getting 400 bad request saying that my file is missing yet when i look at the request i can see all the required files.

Flutter code:

 Future<bool> uploadArtefacts(String objectId, String objectType, List<Uint8List> artefacts) async {
    var request = http.MultipartRequest(
        'POST', Uri.parse(uriString));

    for (var artefact in artefacts) {
      request.files.add(http.MultipartFile.fromBytes('Artefacts', artefact));
    }

    request.fields['objectType'] = objectType;
    request.fields['objectId'] = objectId;
    try {
      var streamedResponse = await request.send();
      var response = await http.Response.fromStream(streamedResponse);
      if (response.statusCode == 204) {
        return true;
      }
    } catch (e) {
      log(e.toString());
    }

    return false;
  }

My controller

[HttpPost]
public async Task<IActionResult> UploadArtefact([FromForm] ArtefactDTO artefactDTO)
{
   if (!ModelState.IsValid)
   return BadRequest(ModelState);
   /// omitted for brevity
    
   return NoContent();
}

ArtefactDTO

public record ArtefactDTO(
      [Required]
      string ObjectType,
      Guid ObjectId,
      [Required]
      List<IFormFile> artefacts,
      string Description);

Request output

enter image description here

Response

"errors":{"artefacts":["The artefacts field is required."]}


Solution 1:[1]

For some reason Multipart class wasn't creating full formed multipart files using the fromBytes method.

Resolved it by changing it to fromPath, it worked fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aeseir