Form uploaded to image

2

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

    
asked by anonymous 12.09.2014 / 22:42

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");
     }
    
12.09.2014 / 22:58