I am looking for a way to attach parameters form along with a file using FormData.
so I have a file input and a form with many parameters.
this is how I send my file:
let formData=new FormData();
formData.append("file",<File>this.file);
this is my form:
this.fileForm = this.fb.group({
headline: [],
fileType: [],
fileCategory: [],
fileEssence: []
});
I tried to add the form value like this:
formData.append("params",this.fileForm.value);
but I can't find it in the c# code:
var form= Request.Form;
only brings the file object.
Is there another way to do this?
update:
this is my upload function in angular:
save(){
let formData=new FormData();
formData.append("file",<File>this.file);
formData.append("parameters",this.fileForm.value);
this.fileService.uploadFile(formData).subscribe((res)=>{
console.log(res);
})
}
file service:
uploadFile(fileUpload: any): Observable<any> {
return this.http.post(ApiConsts.uploadFile, fileUpload).pipe(
map((res: any) => res))
}
C# function:
public IActionResult UploadFile([FromForm] FileUploadDTO parameters, [FromForm] IFormFile file)
{
}
FileUploadDTO class:
public class FileUploadDTO
{
public FileCategory fileCategory { get; set; }
public FileEssence fileEssence { get; set; }
public FileType fileType { get; set; }
public string headline { get; set; }
}
the parameters var is empty, not null but all values are null.