Save Image Path to the database and call in View Asp.Net MVC

Asked

Viewed 322 times

1

I’m trying to save the image path in the bank, and then call in the view, but it’s saving the entire physical path. and at the time of calling, does not appear the image.

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’d like you to save only the way so: ~/Imagens/Blog/NomedaImagem.Extensão but you’re saving it so:

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>

1 answer

1


Remove the

Path.Combine(HttpContext.Server.MapPath(

and use the

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

The use of Httpcontext.Server.Mappath makes it possible to recover the physical path of your file and Path.Combine the root of the physical path with the rest of the path.

See more HERE and HERE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.