Skip to main content

Uploading a File in ASP.NET MVC3

I've done my research on this.  I've seen the literally hundreds of tutorials on how to upload a file in Microsoft's MVC platform.  So, let's do the bit that all of them show first.

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    @Html.Hidden("id", Model.Id)
    <input type="file" name="file" />
    <input type="submit" value="Upload" />

So the first thing to note here is that this is Razor.  There isn't an HtmlHelper for a file input, so we have to add one manually.  Same for the submit button.  Then we notice that I'm using Html.BeginForm, not Ajax.BeginForm.  Everyone loves AJAX right?  Well, AJAX doesn't allow file uploads by default, so if you want to do uploads via AJAX you'll need a plugin for jquery.  I'm submitting via POST for obvious reasons, you do not want your file's byte array in your URI.  The encoding type matters here for two reasons.  First, any misspelling or incorrect case and the whole thing breaks.  Second, multipart/form-data is the only way to pass a file via a form.  Finally, you might ask why I'm passing an ID.  After all, the purpose here is just to upload the file, right?  Since this form is self-contained, we have no way to associate the file with any other data, so you need to pass some identifier of the model you're dealing with in order to make this whole thing work.  For good measure, here's what the action method should look like in the Home controller.

public ActionResult Upload(HttpPostedFileBase file, int id)


    // Do stuff with file  


This is all you need, according to those other tutorials, in order to get file uploads working.  Of course, when I put this into my project and tried to run it, I ran into something odd.  When I tried to upload a file, it went to the Upload URL, but never hit the controller action.  When I clicked upload without having selected a file, it went to the controller action, but naturally would fail.  I couldn't figure it out, so after an hour I got to the point of opening fiddler and got myself a nice clean 504 error.  Ah yes, my web.config wasn't yet allowing the larger quantity of data to pass over the wire.  I'd be upset with myself, but I was more upset with all the tutorials that ignored this obvious bit.
<httpRuntime maxRequestLength="102400"/>
That's what I was missing.  Add it to the system.web node of your web.config and the uploads won't mysteriously disappear.


Popular posts from this blog

How to identify a skilled programmer during an interview

How does one identify a skilled programmer?  No company that has interviewed me could tell the difference between myself and other programmers they'd interview.  The interview process is truly a game of luck in this industry--on both sides.  Both the programmer and the company are basing their actions entirely on luck.

Companies have come up with numerous methods to attempt to discern a good programmer from a bad one.  The best tricks they have include a series of math problems, algorithms, problem solving technique tests, and even obscure programming questions, some without definitive answers.  As an example: Is there an authoritative source of information on the core principles that define object oriented programming?  I've heard everywhere from 3 to 7.  In a field of research about a synthetic concept, an authoritative answer is almost impossible to obtain.

Programmers were then forced to study to the interview.  Careercup is one of my favorite sites for this.  This almost …

When Is Software Done?

I have some very exciting news.  A piece of software I've been working on for over 2 years is released to the general public!  This is a little exciting if it were software I'd been working on for some big company.  It's very exciting because it's software I have been working on for my company.  That's right!  My company is ready to start selling software and start making money!

I'm not gonna use this blog post to talk about my company and what it does.  You can read about that in our press release.  Instead, I'm going to talk about the software industry and the concept of done.  Because, as with everything, it's more complicated than it seems.

Software is never really done
Actually that's a misnomer.  Software can really be done.  But done is sort of a quantum state--there and not there at the same time.  First and foremost, anyone can understand that software that works is complete.  If the software's purpose is to process a credit card, if th…

Managing Programmers

Working with other programmers is tricky.  That said, it's nothing compared to the job of managing programmers.  One of my favorite quotes about Perl is that (paraphrased) "a Perl developer is like a rockstar.  Now imaging having a bunch of rockstars in one room together and you will understand why you don't want an entire team of Perl developers."  It's not about Perl here though. What's important to understand is that any developer worth his salt is going to be like a rockstar.  And yes, there are a lot of professional developers out there who aren't worth their salt, but that's for another post another day.  Rockstar may not be the right term here, but think of it this way.  These guys are smart.  They may not be geniuses, but there's going to be things that they know that you don't and probably never will.

I've seen it more than once and it's not going to make some Product Managers happy, but I'm going to state a fact, an eleph…