Uploading File in ASP.NET MVC

Uploading file in ASP.NET MVC is not as straight job as in ASP.NET as you have to do the ASP.NET FileUpload server control job yourself. You have to follow following simple steps.

1.  Set enctype property of the form to "Multipart/form-data" as shown below. By default enctype of form is “application/x-www-form-urlencoded"

image

The page contains an input field of type “file” and button to submit the form.

By adding setting enctype to "Multipart/form-data", HTTP takes cares of the files uploaded before submission.

2. Add logic to grab the file in the controller that is being used to update the form say it’s “Edit”. When the form having enctype = "Multipart/form-data" is submitted the Request object will contain a collection of files, iterate this collection to get the desired file.

controller-fileupload

That’s it.

Just for your info you might have seen that the built in HttpPostedFileBase class plays key role in getting correct file’s content type and saving it. This class has following members that almost all we have used in this example!

image

Hope it helps.

ASP.NET MVC vs ASP.NET

I have been working on an application in ASP.NET MVC for last few months. It was in Beta then, now Microsoft has released its first version and also made it open source under MS-PL. I found it a great framework for web development as it provides you full control over all ins and outs of the request & response over http. It utilizes HTTP to its full extent. You will definitely go for ASP.NET MVC instead of regular ASP.NET if want to achieve following objectives. ASP.NET MVC 1. You want more control over Request and Response. No more ViewState and Postback. You have full control over the rendered HTML. 2. You want to make your urls RESTful, i.e. your site is capable to have a unique url for each action associated with each item. ASP.NET MVC gets this influence from RESTful web services. And RESTful urls are definitely SEO friendly. 3. You want clean separation of concerns. 4. You want to bring parallelism in development, i.e. one developer may work on View and another may work on controller and so on. This boosts productivity. 5. ASP.NET MVC is best suited for Test Driven Development (TDD) as it is contract based (interface based) and is easily mockable. 6. ASP.NET MVC provides fine grain control over any javascript framework like jQuery, Prototype etc. quite easily and intuitively. 7. ASP.NET MVC is easily pluggable and extendable framework. 8. Above all, now ASP.NET MVC is open source under MS-PL. So you have full source code to change anything you want. ASP.NET Though ASP.NET provides you lot of advantages, regular ASP.NET still will remain in industry and has its own worth and importance. 1. Provides a higher level of abstraction on Request and Response, brings ViewState and Postback in the cycle. Thus good option for novice web developers! 2. Well suited for heavy data driven applications. 3. Provides you set of server controls (built in & third party) that helps you boost productivity. In my upcoming posts I will show you how to work with ASP.NET MVC using JQuery. JQuery is best suited Javascript framewok for ASP.NET MVC to work with. We'll see almost all important features from DOM scripting and UI to rich Ajax calls, related with all types of form posting scenarios.