Here is my action that leads to a view, in which you upload a file. The file then is stored in my database as a byte array.
[HttpPost]
public async Task<IActionResult> Upload(ImageViewModel model)
{
byte[] data = null;
using (var ms = new MemoryStream())
{
await model.Data.CopyToAsync(ms);
data = ms.ToArray();
}
var picture = new Picture()
{
Name = model.Name,
Image = data
};
await context.Images.AddAsync(picture);
await context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
Here is my upload page view:
@model ImageViewModel
<form asp-controller="Image" asp-action="Upload" method="post" enctype="multipart/form-data" >
<div class ="form-group">
<label asp-for="@Model.Name" class="control-label"></label>
<input type="text" asp-for="@Model.Name"/>
</div>
<div class="form-group">
<label asp-for="@Model.Data" class="control-label"></label>
<input type="file" asp-for="@Model.Data" />
</div>
<div class="form-group">
<input type="submit" value="Upload!" class="btn btn-primary" />
</div>
</form>
My question is what are the ways I can display the image on a view in my MVC project?
Do I need to convert it to a IFormFile or is there another way like converting it to a base64 string?
3
Answers
You have two most common options:
1- You can read the byte array from database and convert it to base64 and set it as
2- You can create an new action (e.g. file) which has "id" parameter, as below:
My recommendation would be the later one, because it is more flexible and practical than the first one.
You can use the following code to display the byte array as image in the view:
Note: But if your images’ format are all different, you need add an extra property to store the format. You could refer to this answer. The second option in this answer would help you.
A simple working demo you could follow:
Model
View(Index)
Controller
you can save image on a direct path in your server and In order not to have the same name generate a Guid name for image
you should save image Guid in database and show image in view like this