Whenever I create a form with a file upload field and try to get the file information to bind to my view model I run into a snag. It’s really easy to do though; here are the steps:
Your view model should look something like this:
HttpPostedFileBase will be the property that holds our file upload information. This should bind automatically but we need to make sure our form is set up right. It’s important that our form as an enctype of
Using Razor it would look like this:
I had some trouble using Razor to generate the actual file upload control so I just use plain HTML like this:
In the controller we should now have the file upload information binded to the posted model. Using this information should look something like this:
And there you have it. I hope that works out for you.
Understanding how a programming language works is a surefire way to be a better developer. Read more.