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.

Comments

Popular posts from this blog

Managing Developers is HARD

I've been a software dev for a long time.  I've also been running my own software company for a few years now.  This is important information because of why I do these things.  I am a sofware developer because I love learning.  I slack off when doing a job that bores me, and software development always has something new to experience which keeps me excited and interested.  Why start a software company then?  That puts me in the role of manager rather than developer.  The truth is simple.  I've worked for a lot of companies, and I don't see any of them doing a great job of managing their software development.  That's not to say none of them have done a good job, but no one out there seems to be doing a great job.

How are they different?
A lot of companies get this part right.  Software developers are different from other employees.  The distinction is important in the same way it's important to acknowledge that an insurance agent is different from a construction…

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…

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 …