Uploading an image to a GET endpoint is not a good idea because they have no request body and are not supposed to change the server state (MDN).
Instead, I would recommend you to use a POST endpoint and data binding to IFormFileCollection. I am using this inside of a standard MVC controller but don't know whether it works in an ApiController, too.
[HttpPost("imageUploadEndpoint")]
public async Task<IActionResult> Upload(IFormFileCollection files)
{
foreach (IFormFile file in files)
{
MemoryStream ms = new MemoryStream();
await file.CopyToAsync(ms);
}
return Ok();
}
In one of my open source projects you can find the full implementation of an image upload with ASP.NET Core MVC (GitHub).
Update for storing in database
Although I would recommend to store photos as files, you could create an EF Core database context and an entity like this:
public class FileDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<File>().HasKey(f => f.Id);
}
}
public class File
{
public int Id { get; set; }
public byte[] Data { get; set; }
}
Now you can extend your controller:
private readonly FileDbContext db; // Use constructor injection
[HttpPost("imageUploadEndpoint")]
public async Task<IActionResult> Upload(IFormFileCollection files)
{
foreach (IFormFile file in files)
{
using MemoryStream ms = new MemoryStream();
await file.CopyToAsync(ms);
File dbFile = new File { Data = ms.ToArray() };
db.Set<File>().Add(dbFile);
}
await db.SaveChangesAsync();
return Ok();
}