c# mvc .net core program to upload images code example

Example 1: validating file upload asp.net core mvc

public class UserViewModel
{
        [Required(ErrorMessage = "Please select a file.")]
        [DataType(DataType.Upload)]
        [MaxFileSize(5* 1024 * 1024)]
        [AllowedExtensions(new string[] { ".jpg", ".png" })]
        public IFormFile Photo { get; set; }
 }

Example 2: asp net core image server

[HttpPost]
public ActionResult RetrieveImage()
{
    Image img = db.Images.OrderByDescending
(i=>i.Id).SingleOrDefault();
    string imageBase64Data = 
Convert.ToBase64String(img.ImageData);
    string imageDataURL = 
string.Format("data:image/jpg;base64,{0}", 
imageBase64Data);
    ViewBag.ImageTitle = img.ImageTitle;
    ViewBag.ImageDataUrl = imageDataURL;
    return View("Index");
}