Save Path of the image in the bank and call in the View Asp.Net MVC

1

I'm trying to save the image path in the database, and then calling the view, however, is saving the entire physical path. and at the time of calling, the image does not appear.

My Controller:

 public ActionResult Create(IEnumerable<HttpPostedFileBase> file)
        {
            ImagensBlog imagem = new ImagensBlog();
            var path = "";
            if (file != null)
            {
                foreach (var arquivo in file)
                {
                    if (arquivo.ContentLength > 0)
                    {
                        if (Path.GetExtension(arquivo.FileName).ToLower() == ".jpg"
                          || Path.GetExtension(arquivo.FileName).ToLower() == ".png"
                              || Path.GetExtension(arquivo.FileName).ToLower() == ".gif"
                          || Path.GetExtension(arquivo.FileName).ToLower() == ".jpeg")
                        {

                            path = Path.Combine(HttpContext.Server.MapPath("~/Imagens/Blog"), arquivo.FileName);
                            arquivo.SaveAs(path);
                            ImagensBlog img = new ImagensBlog();
                            img.UrlDaImagem = path;
                            img.DataSalvamento = DateTime.Now;
                            ViewBag.UploadSuccess = true;
                            imagem.Salvar(img);
                            //db.ImagensBlogs.Add(img);
                        }
                    }


                }
                ViewBag.msg = "Salvo Com Suceso";
                return View();
            }

            ViewBag.msg = "Não foi possivel";
            return View();
        }

I would like to save only the path like this: ~/Imagens/Blog/NomedaImagem.Extensão but you're saving it like this:

C:\Users\MeuUsuario\source\repos\E-Commerce\E-Commerce\Imagens\Blog_MG_9355.jpg 

My View:

@model IEnumerable<E_Commerce.Models.Loja.ImagensBlog>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UrlDaImagem)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DataSalvamento)
        </th>
        <th>
           Imagem
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UrlDaImagem)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DataSalvamento)
        </td>
        <td>
            <img src="@item.UrlDaImagem" />
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

</table>
    
asked by anonymous 26.10.2018 / 01:06

1 answer

1

Remove the

Path.Combine(HttpContext.Server.MapPath(

and use the

"~/Imagens/Blog/" + arquivo.FileName;

Using the HttpContext.Server.MapPath causes the physical path of your file to be recovered, and Path.Combine combines the root of the physical path with the rest of the path.

See more HERE and HERE

    
26.10.2018 / 15:07