Sending file from Angular frontend to ASP.NET Core keeps giving me an error

Viewed 41

I'm trying to send a file from an input box at my Angular frontend to a backend in ASP.NET Core, but keep running into a failure. I'm kind of lost right now, having tried a lot of options trying to solve this. Hopefully someone here can give me a hand!

This is my angular code from a service that receives the file from the input box and the sends it towards my API:

upload(file: File): void {
console.log(file);
this.http.post<File>('https://localhost:7125/api/courses', file, this.httpOptions)
  .subscribe((response) => {
    console.log(response);
  })}

This is my ASP.NET Core API which doesn't have a lot of things to work with, I'm just testing out and seeing if the file arrives at all:

[HttpPost]
    public void UploadFile(IFormFile file)
    {
        if (file != null)
        {
            Console.WriteLine(file);
        }
        
    }

Error

The content of the file I'm trying to get towards my backend actually starts with a 'T', so I guess that's something of the error.

Test: Value1
Test1 : Value2

Test: Value3
Test1: Value4
1 Answers

I managed to fix it by setting the 'file' in my post method to { file : file } (converting it to Json) and having my method in C# accept a DTO input value, whereas the DTO had a property called likewise

Related