I have an image that is in base64. It was encoded with Convert.ToBase64String(Image)
in my controller. My question is, by using JavaScript, how can I insert the already loaded image from img element into the input element so I can receive it in the controller. I think I need to decode it somehow, create a new image file and afterwards push that file in input.files, but after numerous tries I couldn't find how. I don't have a lot of experience in javascript, so I would appreciate it if someone helps me out.
<img id="mainImage" src="data:image;base64,@Model.ImageInBase64">
<input asp-for="Image" id="mainImageInput" style="display:none" type="file" onchange="readURL(this,'mainImage')" accept="image/jpeg, image/png" class="form-control"/>
My view model has these 2 properties:
[Required]
public IFormFile Image { get; set; }
public string? ImageInBase64 { get; set; }
For more context, I am saving all my images in a database as byte[].