Answer the question
In order to leave comments, you need to log in
How to display multiple images from file system in .Net project?
Hello everyone, I need your help with working with images in .Net. In general, I do something like CRUD operations only with images. Each name corresponds to several image files ( photo )
Now when we click Edit, this form appears
And as you can see, the name of the photo files that I uploaded is displayed. All files go to the Upload folder in the project when uploading, and information about them is in the database. So the question is: is it possible to display the images themselves instead of the name of the images ???
Here is a code snippet that displays just the name of the uploaded files
@using (Html.BeginForm("Edit", "Support", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit Support Request</legend>
@Html.HiddenFor(model => model.SupportId)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Summary)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Summary)
@Html.ValidationMessageFor(model => model.Summary)
</div>
<div class="editor-label">
<label>Files:</label>
</div>
<div class="editor-field">
<input type="file" name="file" multiple="multiple" />
<ul class="attachment">
@foreach (var item in Model.FileDetails)
{
<li>
<a class="title" href="/Support/Download/[email protected](item.Id + item.Extension)&[email protected]">@item.FileName</a>
<a href="javascript:void(0);" data-id="@item.Id" class="deleteItem">X</a>
</li>
}
</ul>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
Answer the question
In order to leave comments, you need to log in
Controller:
private readonly IApplicationEnvironment _appEnvironment;
public YourController(IApplicationEnvironment appEnvironment)
{
_appEnvironment = appEnvironment;
}
public ActionResult Create()
{
YourModel model = new YourModel();
string path = _appEnvironment.ApplicationBasePath + "\\wwwroot\\Content\\Default"; // получим путь к файловой системе
model.Photo = ImageToByte(path + "\\Image.png");
}
private byte[] ImageToByte(string path)
{
Image image = Image.FromFile(path);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
return memoryStream.ToArray();
}
@Html.Raw("<img style='width:50px;' src=\"data:image/jpeg;base64," + Convert.ToBase64String(Model.Photo) + "\" />")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question