How to parse byte[] array from model with IFormFil

2019-07-23 03:54发布

问题:

I have Core MVC application, when for specific entity I have following model:

public class Aktualnosci
    {
        public long ID { get; set; }
        public string Tytul { get; set; }
        public string Tresc { get; set; }
        public DateTime Dzien { get; set; }
        public byte[] AktualnosciImage { get; set; }
    }

For uploading the image I am using IFormFile property in my viewmodel, that is called by controller, according to the documentation from >>HERE<<:

public class AktualnosciCreateVM
    {
        public long ID { get; set; }
        [Required(ErrorMessage = "Proszę wypełnić pole.")]
        [StringLength(40, ErrorMessage = "Max 40 znaków.")]
        public string Tytul { get; set; }
        [Required(ErrorMessage = "Proszę wypełnić pole.")]
        public string Tresc { get; set; }
        [Required(ErrorMessage = "Proszę wypełnić pole.")]
        public DateTime Dzien { get; set; }
        public IFormFile AktualnosciImage { set; get; }
    }

And it is used for creating and editing of the entity. Right now I have troubles with parsing public IFormFile AktualnosciImage { set; get; } and public byte[] AktualnosciImage { get; set; } in my controller's GET method, to have returned viewmodel:

[Authorize(Roles = "Moderatorzy")]
        // GET: Aktualnosci/Edit/5
        public IActionResult Edit(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            Aktualnosci aktualnosci = aktualnosciRepository.AktualnosciList
                .FirstOrDefault(m => m.ID == id);
            if (aktualnosci == null)
            {
                return NotFound();
            }
            else
            {
                aktualnosciCreateVM.ID = aktualnosci.ID;
                aktualnosciCreateVM.Tytul = aktualnosci.Tytul;
                aktualnosciCreateVM.Tresc = aktualnosci.Tresc;
                aktualnosciCreateVM.Dzien = aktualnosci.Dzien;
//this one gives me an error v
                aktualnosciCreateVM.AktualnosciImage = aktualnosci.AktualnosciImage.ToArray();
                return View(aktualnosciCreateVM);

            }
        }

The compilation error is:

Cannot implicitly convert type 'byte[]' to 'Microsoft.AspNetCore.Http.IFormFile'

Is there any way to parse this 2 properties?