I append data to FormData in my Angular App by this way
const formData = new FormData();
for (let i = 0; i < this.cherry.Berry.length; i++) {
if (this.cherry.Berry[i].Name !== undefined && this.cherry.Berry[i].Name) {
formData.append('Berry[' + i + '][Name]', this.cherry.Berry[i].Name);
}
if (this.cherry.Berry[i].Description !== undefined && this.cherry.Berry[i].Description) {
formData.append('Berry[' + i + '][Description]', this.cherry.Berry[i].Description);
}
if (this.cherry.Berry[i].fileToUploadMain !== undefined && this.cherry.Berry[i].fileToUploadMain !== null
&& this.cherry.Berry[i].fileToUploadMain.length > 0) {
for (let f = 0; f < this.cherry.Berry[i].fileToUploadMain.length; f++) {
formData.append('`Berry`[' + i + '][ImageList]', this.cherry.Berry[i].fileToUploadMain[f]);
}
}
}
public class CherryDTO
{
public Guid Id { get; set; }
public List<Berry> Berry { get; set; }
}
public class Berry
{
public string Name { get; set; }
public string Description { get; set; }
public List<IFormFile> ImageList { get; set; }
}
[HttpPost]
public async Task<IActionResult> CreateCherry([FromForm] CherryDTO cherryDto)
{
return null
}
In my backEnd I have a model
This example works good, I got everything in back end controller but ImageList null. Basically it works good when I move ImageList to CherryDTO by this way
public class CherryDTO
{
public Guid Id { get; set; }
public List<Berry> Berry { get; set; }
public List<IFormFile> ImageList { get; set; }
}
And append for test by this way
if (this.cherry.Berry[i].fileToUploadMain !== undefined && this.cherry.Berry[i].fileToUploadMain !== null
&& this.cherry.Berry[i].fileToUploadMain.length > 0) {
for (let f = 0; f < this.cherry.Berry[i].fileToUploadMain.length; f++) {
formData.append('ImageList', this.cherry.Berry[i].fileToUploadMain[f]);
}
}
In this way it works good. In my back end I get Files in array.
So the problem in Berry[' + i + '][ImageList]
But it works good with Berry[' + i + '][Name] and Berry[' + i + '][Description]
How I can fix it?