Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/01/2014, 08:44
calbo
 
Fecha de Ingreso: agosto-2013
Mensajes: 10
Antigüedad: 10 años, 8 meses
Puntos: 0
Subir o Adjuntar Archivo en MVC 4 RAZOR

Amigos necesito de su ayuda, tengo una aplicación en MVC4 Razor y estoy intentado subir un archivo con <input type="file" name="archivo" /> pero no me resulta, adjunto código:

@using (Html.BeginForm())
{
// @Html.ValidationSummary("No fue posible crear la Persona") esto es para mostrar el mensaje arriba en general.
<fieldset>
<legend>Formulario de Registro</legend>
<ol>
<li>
@Html.LabelFor(m => m.Nombre)
@Html.TextBoxFor(m => m.Nombre)
@Html.ValidationMessageFor(m => m.Nombre)
</li>
<li>
@Html.LabelFor(m => m.Apellidos)
@Html.TextBoxFor(m => m.Apellidos)
@Html.ValidationMessageFor(m => m.Apellidos)
</li>
<li>
@Html.LabelFor(m => m.Edad)
@Html.TextBoxFor(m => m.Edad)
@Html.ValidationMessageFor(m => m.Edad)
</li>
<li>
@Html.LabelFor(m => m.Comuna)
@Html.TextBoxFor(m => m.Comuna)
@Html.ValidationMessageFor(m => m.Comuna)
</li>
<li>
@* @Html.TextBox("file", "", new { type = "file" }) *@
<input type="file" name="archivo" /> <input type="submit" value="Cargar" name="boton" />
</li>
</ol>
<input type="submit" value="Registrar" name="boton"/>
<input type="submit" value="Volver" name="boton" />
<input type="submit" value="Llenar" name="boton" />
<input type="submit" value="limpiar" name="boton" onclick="return limpiar(); return false;"/>
@* <input type="submit" value="limpiar" name="boton"/>*@
</fieldset>

Y luego en el controlador recibo:

[HttpPost]
public ActionResult Cargar(HttpPostedFileBase archivo)
{
var file = Request.Files[0];

if (archivo != null && archivo.ContentLength > 0)
{
var nombreArchivo = Path.GetFileName(archivo.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Documentos Subidos"), nombreArchivo);
archivo.SaveAs(path);
}

return View();
}

Mi Problema es que el parametro "archivo" viene siempre nulo o vació, alguien sabe por que puede estar pasando esto?? espero sus comentarios, gracias.