1

I am posting multiple files to my controller but it does not seem to find them. It says that files variable is null. I can't understand where is the problem.

HTML

<form action="/gallery/create" enctype="multipart/form-data" method="post" novalidate="novalidate">
    <div class="file-holder">
        <div>
            <input type="file" name="files[]">
            <input type="file" name="files[]">
        </div>
    </div>
    <input type="submit" value="Create">
</form>

Controller

[HttpPost]
public ActionResult Create(Gallery g, IEnumerable<HttpPostedFileBase> files)
{
    string t = string.Empty;
    foreach (var file in files)
    {
        t += file.FileName;
    }

    return Content(t);
}

1 Answer 1

2

Remove the [] from the name:

<input type="file" name="files">
<input type="file" name="files">
Sign up to request clarification or add additional context in comments.

3 Comments

Did it work for your Qmal? I couldn't get this to work on a test. Pretty neat if it did though.
Yea it did, I never knew you can do it like that.
You might find the following bog post from the Haacked useful: haacked.com/archive/2010/07/16/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.