How to parse byte[] array from model with IFormFile from viewmodel for file uploading controller?

∥☆過路亽.° 提交于 2019-12-11 13:35:09

问题


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?

来源:https://stackoverflow.com/questions/53297803/how-to-parse-byte-array-from-model-with-iformfile-from-viewmodel-for-file-uplo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!