Passing a HttpPostedFileBase to a controller metho

2020-07-06 09:19发布

问题:

I'm just trying to create a form where I can enter a name and upload a file. Here's the view model:

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

    public string Name { get; set; }

    public HttpPostedFileBase Resume { get; set; }
}

My view:

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

And my controller method:

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

The problem is that when I post to the controller method, the Resume property is null. The Name property gets passed just fine, but not the HttpPostedFileBase.

Am I doing something wrong here?

回答1:

Add the enctype to your form:

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


回答2:

Please add Encoding Type in Form like,

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


回答3:

Add the encoding type in form of view by following code:

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

Add following code in your respective controller,

[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 path will be inside the directory of basedirectory....You can give your own path where you desire to save file