I am working on ASP DOT NET core web api where I need to send multiple attachments. I tried like following
<input type="text" id="txt_firstName" />
<input type="text" id="txt_lastName" />
<input type="file" id="file_TicketManageMent_AttachFile" multiple />
<input type="button" onclick="fnADD()" />
<script>
function fnADD(){
var input = document.getElementById('file_TicketManageMent_AttachFile');
var files = fileList;
var formData = new FormData();
for (var i = 0; i != files.length; i++) {
formData.append("files", files[i]);
}
var mdl = {};
mdl.FirstName = 'Title';
mdl.LastName = 'Short Description';
mdl.Attachments = formData;
$.ajax({
cache: false,
type: 'Post',
contentType: 'application/json',
data: JSON.stringify(mdl),
url: fnApiRequestUri('api/Customer/AddTicket'),
success: function (xhr, ajaxOptions, thrownError) {
}
});
}
</script>
//C# code
[Route("AddTicket")]
[HttpPost]
[Authorize(Roles = MethodsAuthorization.AllRoles)]
public async Task<IActionResult> AddTicket(Model _model)
{
}
public class Model
{
public string FirstName {get;set;}
public string LastName {get;set;}
public List<IFormFile> Attachments { get; set; }
}
I am getting following error the server responded with a status of 400 ()
I referred following question [1]: https://stackoverflow.com/a/49966788/9491935

FormData.FormDatais only applicable formultipart/form-dataencoded requests. Either send theFormDatainstance itself, or you need to read the file data usingFileReaderand then send it in the JSON as a Base64-encoded string. In the latter case, you'll have to bind tobyte[]rather thanIFormFileserver-side, but the serializer will automatically take care of converting from Base64 string tobyte[].