Form with upload to image

Asked

Viewed 566 times

2

How do I upload images to ASP.Net MVC? I have a registration form where the user has the option to send an image to use in his profile. An example would be useful.

1 answer

1


[VIEW]

@using (Html.BeginForm("Create",
                        "Arquivo",
                        FormMethod.Post,
                        new { enctype = "multipart/form-data" }))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <input type="file" name="file" id="file" />
    <button type="submit" class="btn btn-success">Salvar</button>
}

[CONTROLLER]

    [HttpPost]
    public ActionResult Create(FormCollection collecion, HttpPostedFileBase file)
    {
        if (file == null)
        {
            ModelState.AddModelError("Erro", "Nenhum arquivo selecionado.");
            return View();
        }
        string path = Path.Combine(Server.MapPath("~/Arquivos/" + file.FileName));
        return RedirectToAction("Index");
     }
  • Thanks! I can see the name of the image in the database, but I would like to display the image instead of the name. You would know how to do?

Browser other questions tagged

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