0

I have the following form on an .NET MVC View:

<form method="post" enctype="multipart/form-data" action="/Video/UploadDocument"> 
<input type="file" id="document1" name="document1"/> 
<input type="submit"  value="Save"/> 
</form> 

And the controller has the following signature that gets called:

public ActionResult UploadDocument(HttpPostedFileBase file) {
    return View();
}

When I break inside the UploadDocument method, the parameter 'file' is null. I've selected a valid document on my desktop and know it contains text. What am I missing to get this file upload working?

3 Answers 3

2

Try to use

HttpPostedFileBase file = Request.Files["document1"];

There's probably something wrong with bindings ([Bind()] attribute).

Edit: And make that method public ActionResult UploadDocument() {}.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried that and it worked....totally don't get why that fixes it. Wierd. Thanks though!
I only have 1 idea why it began to work: If I remeber, after modifying controller code, the application had to be compiled again for changes to take effect. Did you do that?
1

This has been answered but I believe the culprit is your signature. Instead of "file", use "document1".

public ActionResult UploadDocument(HttpPostedFileBase document1) 
{ 
    return View(); 
} 

Please try and let me know your result

Comments

0

What happens when you make the name of the argument (file) and the name of the input element equal? I recall that the default model binding logic in ASP.NET MVC works using this convention.

1 Comment

Still null. Changed 'file' to 'document1' and still had null. :(

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.