I am passing FormData object from Angular 7 to WebAPI but I getting a null
Any help is appreciated. Thanks
From typescript, i have
selectFile(event){
if (event.target.files[0]) {
this.blob = event.target.files[0];
}
}
save() {
let formDataDTO = new FormData();
formDataDTO.append('file', this.blob, this.fileName);
this.http.post<T>(this.url, JSON.stringify(formDataDTO), this.getHeaders())
.pipe(
catchError(error => {
}))
}
In WebAPI,
[HttpPost]
[Route("file/add")]
public HttpResponseMessage Save([FromBody] HttpPostedFileBase form)
{
var test = form; // form is always null
//cannot access Request.Content.file
// Request.Content.isFormData() is false
// Request.Content.IsMimeMultipartContent() cannot be evaluated
}
try this :
first of all, define a service to send the files :
then create a form to select the files :
here, call the UploadFileSimpleService to send the files after submitting the form :
and finally in your MVC Controller :