【问题标题】:Passing a HttpPostedFileBase to a controller method将 HttpPostedFileBase 传递给控制器​​方法
【发布时间】:2012-03-19 14:49:15
【问题描述】:

我只是想创建一个表单,我可以在其中输入名称并上传文件。这是视图模型:

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

    public string Name { get; set; }

    public HttpPostedFileBase Resume { get; set; }
}

我的看法:

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

还有我的控制器方法:

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

问题是当我发布到控制器方法时,Resume 属性为空。 Name 属性可以很好地传递,但 HttpPostedFileBase 不能传递。

我在这里做错了吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    通过以下代码以视图形式添加编码类型:

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

    在您各自的控制器中添加以下代码,

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

    我假设路径将在 basedirectory 的目录内....您可以提供自己想要保存文件的路径

    【讨论】:

      【解决方案2】:

      请在表单中添加编码类型,

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

      【讨论】:

        【解决方案3】:

        enctype 添加到您的表单中:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-06
          • 2020-05-19
          • 1970-01-01
          • 2014-07-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多