I am trying to upload a form ( few fields along with multiple files attachments ) from angular 8 application to .NET core (CORS Enabled). POST API in .net core is accessible from angular when we send no files and without "Multipart/Form-data" content type, In this case fields are available but not files.
If I change the Content-Type to "Multipart/Form-data" and attach the files in formdata angular gives CORS issue. Below is my code :
Angular:
const formData = new FormData();
const options = {
headers: new HttpHeaders({
'Content-Type': 'multipart/form-data',
'Access-Control-Allow-Origin' :'*'
})
}
this.selectedFiles.forEach((f) => formData.append('files', f));}
formData.append('Name',"Test");
this.http.post(url,formData,options).subscribe();
.NET Core:
[HttpPost]
public object UploadData([FromForm] FormModel formModel)
{
// save files and insert data in DB
}