'File upload in a nested json object having multipart file in Angular 6

Hi I am facing problem in uploading file in a object(body:DataSet) while submitting.

My classes are as below:

export class DataSet implements IDataSet {
Id?: number | undefined;
Name?: string | undefined;
isCorrect?: boolean | undefined;
data?: Data[] | undefined;
.
.
.
}
export class Data implements IData {
dataId?: number | undefined;
option?: string | undefined;
isDone?: boolean | undefined;
expectedFile: string|File|undefined;//file is to be sent here
.
.
.}

I want to send the file in this object -body.

I have tried using formdata but dont know how to append Data[] and the file in expectedFile along with Dataset. Also I tried using JSON.stringify but that produces empty object for Data.

Trying to upload it through angular in ui to server side java. Please guide with proper solution.



Sources

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

Source: Stack Overflow

Solution Source