2012-03-01 36 views
8

Solo intento crear un formulario donde pueda ingresar un nombre y cargar un archivo. Aquí está el modelo de vista:Pasar un HttpPostedFileBase a un método de controlador

public class EmployeeViewModel 
{ 
    [ScaffoldColumn(false)] 
    public int EmployeeId { get; set; } 

    public string Name { get; set; } 

    public HttpPostedFileBase Resume { get; set; } 
} 

Mi opinión:

@using (Html.BeginForm("Create", "Employees", FormMethod.Post)) 
{ 
    @Html.TextBoxFor(model => model.Name) 

    @Html.TextBoxFor(model => model.Resume, new { type = "file" }) 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 

    @Html.ValidationSummary() 
} 

Y mi método de control:

[HttpPost] 
public ActionResult Create(EmployeeViewModel viewModel) 
{ 
    // code here... 
} 

El problema es que cuando envío al método de controlador, la propiedad de reanudación se nulo. La propiedad Name se pasa muy bien, pero no HttpPostedFileBase.

Estoy haciendo algo mal aquí?

Respuesta

8

Añadir la enctype a su formulario:

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

favor añadir Tipo de codificación en forma tal que,

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

Agregar el tipo de codificación en forma de vista de código siguiente:

@using (Html.BeginForm("Create", "Employees", FormMethod.Post,new{ enctype="multipart/form-data"})) 
{ 
    @Html.TextBoxFor(model => model.Name) 

    @Html.TextBoxFor(model => model.Resume, new { type = "file" }) 

    <p> 
    <input type="submit" value="Save" /> 
    </p> 
@Html.ValidationSummary() 
} 

Agregue el siguiente código en su controlador respectivo,

[HttpPost] 
public ActionResult Create(EmployeeViewModel viewModel) 
{ 
     if (Request.Files.Count > 0) 
     { 
      foreach (string file in Request.Files) 
      { 
       string pathFile = string.Empty; 
       if (file != null) 
       { 
        string path = string.Empty; 
        string fileName = string.Empty; 
        string fullPath = string.Empty; 
        path = AppDomain.CurrentDomain.BaseDirectory + "directory where you want to upload file";//here give the directory where you want to save your file 
        if (!System.IO.Directory.Exists(path))//if path do not exit 
        { 
         System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "directory_name/");//if given directory dont exist, it creates with give directory name 
        } 
        fileName = Request.Files[file].FileName; 

        fullPath = Path.Combine(path, fileName); 
        if (!System.IO.File.Exists(fullPath)) 
        { 

         if (fileName != null && fileName.Trim().Length > 0) 
         { 
          Request.Files[file].SaveAs(fullPath); 
         } 
        } 
       } 
      } 
     } 
} 

I asssumed camino será dentro del directorio de basedirectory .... Usted puede dar su propia ruta en la que desea guardar el archivo

+0

1 ¿qué pasa con Html.editor en lugar de Html.TextBoxFor – Nikos

Cuestiones relacionadas