MVC. HttpPostedFileBase is always null

2019-01-17 15:16发布

问题:

I need some help. I'm trying to upload files using <input type="file">. Here is my View:

@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files[0]" id="files[0]" />
    <input type="file" name="files[1]" id="files[1]" />
    <input type="submit" value="Upload Book" />
}

And here is an Action which should process uploaded file.

[HttpPost]
public ActionResult BookAdd(IEnumerable<HttpPostedFileBase> files)
{
    // some actions
    return View();
}

The problem is that "files" always contains two elements which are null. What can be done to fix it?

It's time for some news. It seems like I found the problem, but I still don't know how to fix it. It appears that despite the fact I'm using "multipart/form-data" here:

@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, new { enctype="multipart/form-data" }))
{
    <input type="file" name="File" id="file1" />
    <input type="file" name="File" id="file2" />
    <input type="submit" value="Upload Book" />
}

Request.ContentType remains "application/x-www-forum-urlencoded" in controller..

回答1:

Just get rid of the square brackets in the names of your input fields:

@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="submit" value="Upload Book" />
}

UPDATE:

After looking at the sample project you sent me the problem is that you have 2 nested forms. This is not allowed in HTML. You have one form in your _Layout.cshtml and another form in your BookAdd.cshtml view. That's the reason why despite the enctype="multipart/form-data" attribute on your inner form you were getting the wrong Request.ContentType. So you will have to unnest those forms if you want this to work. Also in the example you sent me your BookAdd controller action doesn't have the correct signature taking a list of files, but I guess that's due to some tests you were doing.



回答2:

The problem is that the NAME of the field need to match with the controller parameter. In your case is "files"... so your name attribute should be "files" also.



回答3:

I was facing same problem but in my case i got a solution of this.

[HttpPost]
    public ActionResult Upload()
    {
        foreach (string file in Request.Files)
        {               
           fileurl = Request.Files[file];
        }
        return View();
    }

In Design View.Please Comment out Form Tag <%-- <form id="form1" runat="server">--%> if using Master Page...I hope Your problem will be solve...

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Upload
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

   <%-- <form id="form1" runat="server">--%>
  <% using (Html.BeginForm("Upload","Home",FormMethod.Post,new {enctype="multipart/form-data"}))
{ %>

<fieldset>
        <legend>Upload File</legend>
         <div>

          <p>
Select a File: <input type="file" name="FileUpload" />
<input type="submit" value="Upload" />
</p>
</div>
</fieldset>

<% } %>


回答4:

Worth noting if you're using AJAX then the upload will always come through as null, so make sure no jquery AJAX is attached to the form submit.



回答5:

You must write some like this:

public class DocumentModelView 
{
        public HttpPostedFileBase File1 { get; set; }

        public HttpPostedFileBase File2 { get; set; }
}


@model Models.DocumentModelView

@using( Html.BeginForm( "Create", "Document", FormMethod.Post, new { enctype = "multipart/form-data" }) )
{
        <input type="file" name="File1" />
    <input type="file" name="File2" />
        <input type="submit" value="send" />
}


[HttpPost]
public ActionResult Create( DocumentModelView modelView )
{
.....
}


回答6:

In my case y had to use the name instead of id.

Like this:

<input type="file" id="upload" name="upload" />