'Net framework 4.8 (API) with Angular2 (12) - upload object and file not
I am sending from my frontend to the backend the following object
{ description: string, image:File }
the function in the service would be the following example
headers: HttpHeaders = new HttpHeaders({
'Content-Type': 'application/json',
// 'Content-Type': 'multipart/form-data',
// 'Content-Disposition': 'form-data',
// 'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,DELETE,PUT'
});
update(body: any): Observable<any> {
const file: File = body.Image;
const formData= new FormData();
formData.append('Image',file, file.name);
return this.http.post<any>(`${this.baseApi}/Update`, formData);
}
the problem is that being the version of net framework, try to use the class HttpPostedFile /HttpPostedFileBase using [FromBody] in the endpoint i think i should use [FromForm] and in angular request header use header 'Content-type':'multipart/form-data'
[Route(nameof(Update))]
[HttpPost]
public IHttpActionResult Update([FromBody]EntityDto dto)
{
UpdateResponse response = new UpdateResponse();
var httpRequest = HttpContext.Current.Request; // not have the file in property file
try
{
response = this.entityRepository.Update(dto);
}
catch (GenericException ex)
{
GenericException.ThrowException(ex);
}
return Ok(response);
}
the dto instance class has the HttpPostedFile type file property inside and it doesn't work (it doesn't bring the file, would it be correct to send it in some other way or send the byte array? If so, it would be great to see an example)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
