Mvc HttpPostedFileBase返回null

时间:2014-08-02 06:20:07

标签: asp.net-mvc file-upload null return

最近我试图处理HttpPostedFileBase返回null问题,但我无法弄清楚为什么我的输入文件返回null虽然我已经研究了所有类似的问题来了解网站中的问题。我sutiation我需要有四个输入文件,因为我需要四个不同的文件彼此。

这是视图的一部分:

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

<div style="padding-left: 32px;">
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Id)
<table>
        <tbody>
            <tr>
                <td style="width: 132px;"><div><b>...</b></div></td>
                <td style="width:250px;">
                    <input type="file" name="upload1" id="upload1" class="formin" />
                    <input type="hidden" name="UploadType1" id="UploadType1" value="@Request.QueryString["UploadType"]" />
                </td>
            </tr>
            <tr>
                <td style="width: 132px;"><div><b>...</b></div></td>
                <td style="width:250px;">
                    <input type="file" name="upload2" id="upload2" class="formin" />
                    <input type="hidden" name="UploadType2" id="UploadType2" value="@Request.QueryString["UploadType"]" />
                </td>
            </tr>
            <tr>
                <td style="width: 132px;"><div><b>...</b></div></td>
                <td style="width:250px;">
                    <input type="file" name="upload3" id="upload3" class="formin" />
                    <input type="hidden" name="UploadType3" id="UploadType3" value="@Request.QueryString["UploadType"]" />
                </td>
            </tr>
            <tr>
                <td style="width: 132px;"><div><b>...</b></div></td>
                <td style="width:250px;">
                    <input type="file" name="upload4" id="upload4" class="formin" />
                    <input type="hidden" name="UploadType4" id="UploadType4" value="@Request.QueryString["UploadType"]" />
                </td>
            </tr>

        </tbody>
    </table>
    <table>
        <tbody>
            <tr>
                <td style="width:220px;"><div><b></b></div></td>
                <td> <input type="submit" name="submit" value="GÖNDER" class="formbutton" style="font-size: 14px;background:maroon;color:white;"></td>
            </tr>
        </tbody>
    </table>

这是我的控制器:

    [HttpPost]
    public ActionResult Create(HttpPostedFileBase upload1, HttpPostedFileBase upload2, HttpPostedFileBase upload3, HttpPostedFileBase upload4, string UploadType1, string UploadType2, string UploadType3, string UploadType4)
    {
     List<Ek> eks = new List<Ek>();


            if (upload1!=null && upload1.ContentLength > 0)
            {
                var upload1Name= Path.GetFileName(upload1Name.FileName);
                var path1 = Path.Combine(Server.MapPath("~/Documents/.../upload1"), PhotoName);
                upload1.SaveAs(path1);


                Ek e1 = new Ek { 
                UploadType = GetTypeEnum(UploadType1),
                UploadPath = "/Documents/.../.../" + upload1Name
                };
                eks.Add(e1);
            }

            if (upload2!= null && upload2.ContentLength > 0)
            {
                var upload2Name = Path.GetFileName(upload2.FileName);
                var path2 = Path.Combine(Server.MapPath("~/Documents/.../..."), upload2Name);
                upload2.SaveAs(path2);

                Ek e2 = new Ek
                {
                    UploadType = GetTypeEnum(UploadType1),
                    UploadPath = "/Documents/.../.../" + upload2Name 
                };
                eks.Add(e2);
            }

            if (upload3!= null && upload3.ContentLength > 0)
            {
                var upload3Name = Path.GetFileName(upload3.FileName);
                var path3 = Path.Combine(Server.MapPath("~/Documents/.../..."), upload3Name );
                upload3.SaveAs(path3);

                Ek e3 = new Ek
                {
                    UploadType = GetTypeEnum(UploadType1),
                    UploadPath = "/Documents/.../" + upload3Name 
                };

                eks.Add(e3);
            }

            if (upload4!= null && upload4.ContentLength > 0)
            {
                var upload4Name = Path.GetFileName(upload4.FileName);
                var path4 = Path.Combine(Server.MapPath("~/Documents/.../"), upload4Name);
                languagepoint.SaveAs(path4);

                Ek e4 = new Ek
                {
                    UploadType = GetTypeEnum(UploadType1),
                    UploadPath = "/Documents/.../" + upload4Name 
                };

                eks.Add(e4);

            }
        //some stuff here
        return RedirectToAction("Success");
    }

    return View(myview);
   }
    private UploadType GetTypeEnum(string UploadType1)
    {

        Models.UploadType uType= UploadType.MyType;
        switch (UploadType1)
        {
            case "MyType":
            uType = UploadType.MyType;
                break;
                   case "MyType1":
            uType = UploadType.MyType1;
                break;
                   case "MyType2":
            uType = UploadType.MyType2;
                break;
                   case "MyType3":
            uType = UploadType.MyType3;
                break;
            default:
                break;
        }
        return uType;
    }

最后这是我的模态:

    public partial class MyMainClass
{
    public int Id { get; set; }

    public virtual IList<Ek> Ek { get; set; }

}
    public partial class Ek
{
    public int ID { get; set; }

    public UploadType UploadType { get; set; }
    public string UploadPath { get; set; }
}

public enum UploadType
{
    MyType= 0,
    MyType1= 1,
    MyType2= 2,
    MyType3= 3
}

感谢您的所有答案。

1 个答案:

答案 0 :(得分:0)

将您的HttpPost控制器纠正为:

 [HttpPost]
 public ActionResult Create(IEnumerable<HttpPostedFileBase> files)
 {
   if (files.Count() > 0)  // display no of files uploaded
   if (files.Any())  // display true
   if (files.First() == null)  // display "first null"

   return View();
 }

因为您要从视图上传多个文件,所以您必须使用HttpPostedFileBase的IEnumerable并在Create操作上编写HttpPost属性,因为它正在接受您的POST请求。