2012-03-01 17 views
8

Po prostu próbuję utworzyć formularz, w którym mogę wprowadzić nazwę i przesłać plik. Oto model widok:Przekazywanie HttpPostedFileBase do metody kontrolera

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

    public string Name { get; set; } 

    public HttpPostedFileBase Resume { get; set; } 
} 

Moim zdaniem:

@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() 
} 

I moja metoda kontroler:

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

Problemem jest to, że kiedy pisać do metody kontrolera, obiekt Resume jest zero. Właściwość Name jest poprawnie przekazywana, ale nie HttpPostedFileBase.

Czy robię coś nie tak?

Odpowiedz

8

Dodaj enctype do formularza:

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

proszę dodać Kodowanie typu w postaci podobnych

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

Dodaj typ kodowania w formie widzenia przez następujący kod:

@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() 
} 

Dodaj następujący kod w odpowiednim sterowniku,

[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 ścieżka będzie wewnątrz katalogu katalogbazowy .... Można podać własną ścieżkę, gdzie chcecie, aby zapisać plik

+0

+1 co Html.editor zamiast Html.TextBoxFor – Nikos

Powiązane problemy