Add a file input field to your template
<input type="file" #fileInput />
<button (click)="uploadFile();">Upload</button>
Next, navigate to your component
@ViewChild('fileInput') fileInput;
// import httpclient from @angular/common/http
...
public uploadFile(): void {
if (this.fileInput.files.length === 0) {
return; // additional validation may be needed
}
const formData = new FormData();
formData.append('file', this.fileInput.files[0]);
this.http.post('http://my-url/api/my-endpoint', formData).subscribe(...); // standard procedure
}
Now, create an endpoint in your API like this
[HttpPost]
// the parameter here must be named 'file' to match the formdata key
public IActionResult UploadFile([FromBody] IFormFile file)
{
// based on your requirements, you can save it to the filesystem or store it in the database as a byte array
}
If you specify where the file should be saved, I can provide further C# code. However, this is the general process of receiving files from the front-end in your back-end.