I have Angular CLI application and Dot net core 2.0 Web API. which I need to upload the file from Angular to Web API. from Web API to Server. When I use Http its working fine. When using HttpClient, it's not working. Here is my component.ts code:
fileChange(event, grdvalue) {
debugger;
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
const formData: FormData = new FormData();
let GridName = grdvalue;
formData.append('file', file, file.name);
formData.append('Id', this.userId);
formData.append('GridValue', GridName)
this.myService.PostFileToAzure(formData).subscribe(details => {
debugger;
},
(error) => {
debugger;
})
}
}
here is My Service Code:
PostFileToAzure(form) {
debugger;
var body = JSON.stringify(form);
return this.http.post(this.baseApiURL + '/Interpretation/InterpreterFileUpload', form);
}
Here is my Web API code:
[HttpPost("InterpreterFileUpload")]
public async Task<IActionResult> InterpreterFileUpload()
{
try
{
var file = Request.Form.Files[0];
HttpRequest formData = HttpContext.Request;
}
}
How to do send the file using HttpClient From Angular to Web API. Am getting an error in web API like 'Incorrect Content-Type: application/json'
JSON.stringify
your form