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.
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.
[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");
}