M
M
Michael2016-11-04 02:36:47
ASP.NET
Michael, 2016-11-04 02:36:47

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 ) 58779e84b0724cd19cb7924b322938f9.PNG
Now when we click Edit, this form appears
948804ed15f745928b816d2905cb42d2.PNG
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

1 answer(s)
R
Roman Zhidkov, 2016-11-04
@M-Misha-M

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();
}

View:
@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 question

Ask a Question

731 491 924 answers to any question